aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/tests/datatype/datatype_test.cpp
blob: 07a660a5e97b4d645ff90d607e8ea0723574f396 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Unit tests for datatype.

#include <vespa/document/base/field.h>
#include <vespa/document/datatype/arraydatatype.h>
#include <vespa/document/datatype/structdatatype.h>
#include <vespa/document/fieldvalue/longfieldvalue.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/exceptions.h>

using namespace document;

namespace {

TEST("require that ArrayDataType can be assigned to.") {
    ArrayDataType type1(*DataType::STRING);
    ArrayDataType type2(*DataType::INT);
    type1 = type1;
    EXPECT_EQUAL(*DataType::STRING, type1.getNestedType());
    type1 = type2;
    EXPECT_EQUAL(*DataType::INT, type1.getNestedType());
}

TEST("require that ArrayDataType can be cloned.") {
    ArrayDataType type1(*DataType::STRING);
    std::unique_ptr<ArrayDataType> type2(type1.clone());
    ASSERT_TRUE(type2.get());
    EXPECT_EQUAL(*DataType::STRING, type2->getNestedType());
}

TEST("require that assignment operator works for LongFieldValue") {
    LongFieldValue val;
    val = "1";
    EXPECT_EQUAL(1, val.getValue());
    val = 2;
    EXPECT_EQUAL(2, val.getValue());
    val = static_cast<int64_t>(3);
    EXPECT_EQUAL(3, val.getValue());
    val = 4.0f;
    EXPECT_EQUAL(4, val.getValue());
    val = 5.0;
    EXPECT_EQUAL(5, val.getValue());
}

TEST("require that StructDataType can redeclare identical fields.") {
    StructDataType s("foo");
    Field field1("field1", 42, *DataType::STRING, true);
    Field field2("field2", 42, *DataType::STRING, true);

    s.addField(field1);
    s.addField(field1);  // ok
    s.addInheritedField(field1);  // ok
    EXPECT_EXCEPTION(s.addField(field2), vespalib::IllegalArgumentException,
                     "Field id in use by field Field(field1");
    s.addInheritedField(field2);
    EXPECT_FALSE(s.hasField(field2.getName()));
}

}  // namespace

TEST_MAIN() { TEST_RUN_ALL(); }