Blame view

tests/datatypestest.cpp 4.33 KB
a6b8531d   Peter M. Groen   Implemented the V...
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
  /*****************************************************************************
   * Copyright (c)2023 Peter M. Groen
   * This file is licensed under the MIT license found in the LICENSE file
   * in the root directory of this source tree.
   ******************************************************************************/
  
  #include <gmock/gmock.h>
  #include <gtest/gtest.h>
  
  #include "Variant.h"
  
  using namespace osdev::components;
  
  TEST(DataTypesTest, BooleanTest)
  {
      Variant oVariant(true);
      EXPECT_EQ(oVariant.getType(), Variant::Bool);
      EXPECT_TRUE(oVariant.CanConvert(Variant::Bool));
      EXPECT_TRUE(oVariant.toBool());
  }
  
  TEST(DataTypesTest, PositiveIntegerTest)
  {
      int value = 42;
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::Int);
      EXPECT_TRUE(oVariant.CanConvert(Variant::Int));
      EXPECT_EQ(oVariant.toInt(), 42);
  }
  
  TEST(DataTypesTest, NegativeIntegerTest)
  {
      int value = -42;
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::Int);
      EXPECT_TRUE(oVariant.CanConvert(Variant::Int));
      EXPECT_EQ(oVariant.toInt(), -42);
  }
  
  TEST(DataTypesTest, PositiveDoubleTest)
  {
      double value = 42;
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::Double);
      EXPECT_TRUE(oVariant.CanConvert(Variant::Double));
      EXPECT_EQ(oVariant.toDouble(), 42);
  }
  
  TEST(DataTypesTest, NegativeDoubleTest)
  {
      double value = -42;
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::Double);
      EXPECT_TRUE(oVariant.CanConvert(Variant::Double));
      EXPECT_EQ(oVariant.toDouble(), -42);
  }
  
  TEST(DataTypesTest, PositiveFloatTest)
  {
      float value = 42.00;
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::Float);
      EXPECT_TRUE(oVariant.CanConvert(Variant::Float));
      EXPECT_EQ(oVariant.toFloat(), 42.00);
  }
  
  TEST(DataTypesTest, NegativeFloatTest)
  {
      float value = -42.00;
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::Float);
      EXPECT_TRUE(oVariant.CanConvert(Variant::Float));
      EXPECT_EQ(oVariant.toFloat(), -42.00);
  }
  
  TEST(DataTypesTest, CharTest)
  {
      char value = 'c';
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::Char);
      EXPECT_TRUE(oVariant.CanConvert(Variant::Char));
      EXPECT_EQ(oVariant.toChar(), 'c');
  }
  
  TEST(DataTypesTest, StringTest)
  {
      std::string value = "This is a string";
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::String);
      EXPECT_TRUE(oVariant.CanConvert(Variant::String));
      EXPECT_EQ(oVariant.toString(), "This is a string");
  }
  
  TEST(DataTypesTest, UInt8Test)
  {
      uint8_t value = 0xAA;
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::UInt8);
      EXPECT_TRUE(oVariant.CanConvert(Variant::UInt8));
      EXPECT_EQ(oVariant.toUInt8(), 0xAA);
  }
  
  TEST(DataTypesTest, UInt16Test)
  {
      uint16_t value = 0xAAAA;
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::UInt16);
      EXPECT_TRUE(oVariant.CanConvert(Variant::UInt16));
      EXPECT_EQ(oVariant.toUInt16(), 0xAAAA);
  }
  
  TEST(DataTypesTest, UInt64Test)
  {
      uint64_t value = 0xAAAAAAAAAAAAAAAA;
      Variant oVariant(value);
      EXPECT_EQ(oVariant.getType(), Variant::UInt64);
      EXPECT_TRUE(oVariant.CanConvert(Variant::UInt64));
      EXPECT_EQ(oVariant.toUInt64(), 0xAAAAAAAAAAAAAAAA);
  }
  
  TEST(DataTypesTest, CanConvertTest)
  {
      uint64_t value = 0xAAAAAAAAAAAAAAAA;
      Variant oVariant(value);
      EXPECT_FALSE(oVariant.CanConvert(Variant::Bool));
      EXPECT_FALSE(oVariant.CanConvert(Variant::Int));
      EXPECT_FALSE(oVariant.CanConvert(Variant::Double));
      EXPECT_FALSE(oVariant.CanConvert(Variant::Float));
      EXPECT_FALSE(oVariant.CanConvert(Variant::Char));
      EXPECT_FALSE(oVariant.CanConvert(Variant::String));
      EXPECT_FALSE(oVariant.CanConvert(Variant::UInt8));
      EXPECT_FALSE(oVariant.CanConvert(Variant::UInt16));
      EXPECT_TRUE(oVariant.CanConvert(Variant::UInt64));
  }
  
  TEST(DataTypesTest, GetWrongTypeTest)
  {
      uint64_t value = 0xAAAAAAAAAAAAAAAA;
      Variant oVariant(value);
      EXPECT_ANY_THROW(oVariant.toBool());
      EXPECT_ANY_THROW(oVariant.toInt());
      EXPECT_ANY_THROW(oVariant.toDouble());
      EXPECT_ANY_THROW(oVariant.toFloat());
      EXPECT_ANY_THROW(oVariant.toChar());
      EXPECT_ANY_THROW(oVariant.toString());
      EXPECT_ANY_THROW(oVariant.toUInt8());
      EXPECT_ANY_THROW(oVariant.toUInt16());
      EXPECT_NO_THROW(oVariant.toUInt64());
  }