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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (C) 2015-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <util/optional.h>
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace {

using namespace isc::util;

// This test checks that the constructors work correctly.
TEST(OptionalTest, constructor) {<--- syntax error
    // Explicitly set a value via constructor. The value becomes
    // specified.
    Optional<int> value1(10);
    EXPECT_EQ(10, value1.get());
    EXPECT_FALSE(value1.unspecified());

    // Do not set a value in a constructor. The value should be
    // unspecified.
    Optional<int> value2;
    EXPECT_EQ(0, value2.get());
    EXPECT_TRUE(value2.unspecified());

    // Use the non-default value for second parameter.
    Optional<bool> value3(true, true);
    EXPECT_TRUE(value3.get());
    EXPECT_TRUE(value3.unspecified());
}

// This test checks if the constructors for a string value
// work correctly.
TEST(OptionalTest, constructorString) {
    Optional<std::string> value1("foo");
    EXPECT_EQ("foo", value1.get());
    EXPECT_FALSE(value1.unspecified());

    Optional<std::string> value2;
    EXPECT_TRUE(value2.get().empty());
    EXPECT_TRUE(value2.unspecified());
}

// This test checks if the assignment operator assigning an actual
// value to the optional value works as expected.
TEST(OptionalTest, assignValue) {
    Optional<int> value(10, true);
    EXPECT_EQ(10, value.get());
    EXPECT_TRUE(value.unspecified());

    // Assign a new value.
    value = 111;
    EXPECT_EQ(111, value.get());
    EXPECT_FALSE(value.unspecified());

    // Assign another value.
    value = 1000;
    EXPECT_EQ(1000, value.get());
    EXPECT_FALSE(value.unspecified());
}

// This test checks if the assignment operator assigning an actual
// string value to the optional value works as expected.
TEST(OptionalTest, assignStringValue) {
    Optional<std::string> value("foo");
    EXPECT_EQ("foo", value.get());
    EXPECT_FALSE(value.unspecified());

    value = "bar";
    EXPECT_EQ("bar", value.get());
    EXPECT_FALSE(value.unspecified());

    value = "foobar";
    EXPECT_EQ("foobar", value.get());
    EXPECT_FALSE(value.unspecified());
}

// This test checks that it is possible to modify the flag that indicates
// if the value is specified or unspecified.
TEST(OptionalTest, modifyUnspecified) {
    Optional<int> value;
    EXPECT_TRUE(value.unspecified());

    value.unspecified(false);
    EXPECT_FALSE(value.unspecified());

    value.unspecified(true);
    EXPECT_TRUE(value.unspecified());
}

// This test checks if the type case operator returns correct value.
TEST(OptionalTest, typeCastOperator) {
    Optional<int> value(-10);
    EXPECT_EQ(-10, value.get());
    EXPECT_FALSE(value.unspecified());

    int actual = value;
    EXPECT_EQ(-10, actual);
}

// This test checks if the type case operator returns correct string
// value.
TEST(OptionalTest, stringCastOperator) {
    Optional<std::string> value("xyz");
    EXPECT_EQ("xyz", value.get());
    EXPECT_FALSE(value.unspecified());

    std::string actual = value;
    EXPECT_EQ("xyz", actual);
}

// This test checks that the equality operators work as expected.
TEST(OptionalTest, equality) {
    int exp_value = 1234;
    Optional<int> value(1234);
    EXPECT_TRUE(value == exp_value);
    EXPECT_FALSE(value != exp_value);
}

// This test checks that the equality operators for strings work as
// expected.
TEST(OptionalTest, stringEquality) {
    const char* exp_value = "foo";
    Optional<std::string> value("foo");
    EXPECT_TRUE(value == exp_value);
    EXPECT_FALSE(value != exp_value);
}

// This test checks that an exception is thrown when calling an empty()
// method on non-string optional value.
TEST(OptionalTest, empty) {
    Optional<int> value(10);
    EXPECT_THROW(value.empty(), isc::InvalidOperation);
}

// This test checks that no exception is thrown when calling an empty()
// method on string optional value and that it returns an expected
// boolean value.
TEST(OptionalTest, stringEmpty) {
    Optional<std::string> value("foo");
    bool is_empty = true;
    ASSERT_NO_THROW(is_empty = value.empty());
    EXPECT_FALSE(is_empty);

    value = "";
    ASSERT_NO_THROW(is_empty = value.empty());
    EXPECT_TRUE(is_empty);
}

// Checks that the valueOr function works correctly.
TEST(OptionalTest, valueOr) {
    Optional<std::string> optional("foo");
    EXPECT_EQ(optional.valueOr("bar"), "foo");

    Optional<std::string> unspecified_optional;
    EXPECT_EQ(unspecified_optional.valueOr("bar"), "bar");
}

} // end of anonymous namespace