From a6b8531dcb7aa8d39a69f5cb72d6cdfe2a8e8cf3 Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Wed, 16 Aug 2023 02:44:52 +0200 Subject: [PATCH] Implemented the Variant and tests --- .gitignore | 1 + CMakeLists.txt | 4 ++-- src/Variant.cpp | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------- src/Variant.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++----------------------------- tests/CMakeLists.txt | 28 ++++++++++++++++++++++++++++ tests/datatypestest.cpp | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 330 insertions(+), 71 deletions(-) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/datatypestest.cpp diff --git a/.gitignore b/.gitignore index 9714e1b..35e6677 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /.gitmodules ./build/ build/ +/CMakeLists.txt.user diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e26070..5a695db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,10 +4,10 @@ LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/submodules/cmake) include(projectheader) project_header(datatypes) -# set(REPOSITORY_PACKAGE_NAME ${PROJECT_NAME} CACHE STRING "Repository name for ${PROJECT_NAME}" FORCE) +set(REPOSITORY_PACKAGE_NAME ${PROJECT_NAME} CACHE STRING "Repository name for ${PROJECT_NAME}" FORCE) add_subdirectory(src) # Test applications for each component. -# add_subdirectory(tests) +add_subdirectory(tests) diff --git a/src/Variant.cpp b/src/Variant.cpp index 16a9d4a..e384266 100644 --- a/src/Variant.cpp +++ b/src/Variant.cpp @@ -2,75 +2,137 @@ using namespace osdev::components; -Variant::Variant() -{ -} - -Variant::Variant(Type type) -{ -} - -Variant::Variant(bool value) -{ -} - -Variant::Variant(int value) -{ -} - -Variant::Variant(uint value) -{ -} - -Variant::Variant(double value) -{ -} +Variant::Type Variant::getType() +{ + if(std::holds_alternative(m_variable)) + { + return Variant::Type::Bool; + } + else if(std::holds_alternative(m_variable)) + { + return Variant::Type::Int; + } + else if(std::holds_alternative(m_variable)) + { + return Variant::Type::Double; + } + else if(std::holds_alternative(m_variable)) + { + return Variant::Type::Float; + } + else if(std::holds_alternative(m_variable)) + { + return Variant::Type::Char; + } + else if(std::holds_alternative(m_variable)) + { + return Variant::Type::String; + } + else if(std::holds_alternative(m_variable)) + { + return Variant::Type::UInt8; + } + else if(std::holds_alternative(m_variable)) + { + return Variant::Type::UInt16; + } + else if(std::holds_alternative(m_variable)) + { + return Variant::Type::UInt64; + } -Variant::Variant(float value) -{ + return Variant::Type::Invalid; } -Variant::Variant(char value) +bool Variant::CanConvert(Variant::Type typeWanted) { + switch(typeWanted) + { + case Variant::Bool: + { + return std::holds_alternative(m_variable); + } + case Variant::Int: + { + return std::holds_alternative(m_variable); + } + case Variant::Double: + { + return std::holds_alternative(m_variable); + } + case Variant::Float: + { + return std::holds_alternative(m_variable); + } + case Variant::Char: + { + return std::holds_alternative(m_variable); + } + case Variant::String: + { + return std::holds_alternative(m_variable); + } + case Variant::UInt8: + { + return std::holds_alternative(m_variable); + } + case Variant::UInt16: + { + return std::holds_alternative(m_variable); + } + case Variant::UInt64: + { + return std::holds_alternative(m_variable); + } + case Variant::Invalid: + { + return false; + } + } + return false; } -Variant::Variant(std::string value) +bool Variant::toBool() { + return std::get(m_variable); } -Variant::Variant(uint8_t value) +int Variant::toInt() { + return std::get(m_variable); } -Variant::Variant(uint16_t value) +double Variant::toDouble() { + return std::get(m_variable); } -Variant::Variant(uint32_t value) +float Variant::toFloat() { + return std::get(m_variable); } -Variant::Variant(uint64_t value) +char Variant::toChar() { + return std::get(m_variable); } -Variant::Variant(int8_t value) +std::string Variant::toString() { + return std::get(m_variable); } -Variant::Variant(int16_t value) +uint8_t Variant::toUInt8() { + return std::get(m_variable); } -Variant::Variant(int32_t value) +uint16_t Variant::toUInt16() { + return std::get(m_variable); } -Variant::Variant(int64_t value) +uint64_t Variant::toUInt64() { + return std::get(m_variable); } - -Type Variant::getType() -{ - -} \ No newline at end of file diff --git a/src/Variant.h b/src/Variant.h index f7475d5..e028905 100644 --- a/src/Variant.h +++ b/src/Variant.h @@ -1,51 +1,69 @@ -#pragma one +#pragma once +#include #include +#include + namespace osdev::components { + class Variant { public: enum Type { Invalid, // Unknown Bool, // bool - Int, // int (standard) - UInt, // Unsigned int Double, // double Float, // float Char, // char String, // std::string UInt8, // uint8_t UInt16, // uint16_t - UInt32, // uint32_t UInt64, // uint64_t - Int8, // int8_t - Int16, // int16_t - Int32, // int32_t - Int64 // int64_t + Int // int -->> Size determined by platform and compiler }; /*! - * All Constructors available + * All Constructors available. + * The type of the argument determines the internal type of the variant */ - Variant(); - Variant(Type type); - Variant(bool value); - Variant(int value); - Variant(uint value); - Variant(double value); - Variant(float value); - Variant(char value); - Variant(std::string value); - Variant(uint8_t value); - Variant(uint16_t value); - Variant(uint32_t value); - Variant(uint64_t value); - Variant(int8_t value); - Variant(int16_t value); - Variant(int32_t value); - Variant(int64_t value); - - + Variant() : m_variable() {}; + Variant(bool value) : m_variable(value) {}; + Variant(int value) : m_variable(value) {}; + Variant(double value) : m_variable(value) {}; + Variant(float value) : m_variable(value) {}; + Variant(char value) : m_variable(value) {}; + Variant(std::string value) : m_variable(value) {}; + Variant(uint8_t value) : m_variable(value) {}; + Variant(uint16_t value) : m_variable(value) {}; + Variant(uint64_t value) : m_variable(value) {}; + + /// Return the type of the value stored in this variant + Variant::Type getType(); + + /// Check to see if the value can be converted to the desired type. + /// @param - The requested type as an enum + /// @return - true if the conversion can happen. False if not. + bool CanConvert(Variant::Type typeWanted); + + bool toBool(); + double toDouble(); + float toFloat(); + char toChar(); + std::string toString(); + int toInt(); + uint8_t toUInt8(); + uint16_t toUInt16(); + uint64_t toUInt64(); + + + +private: + std::variant m_variable; + }; -} /* End namespace osdev::components */ \ No newline at end of file +} /* End namespace osdev::components */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..da7634e --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,28 @@ +# ***************************************************************************** +# 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. +# ***************************************************************************** +add_executable(datatypestest + datatypestest.cpp +) + + +target_include_directories(datatypestest PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/src + ${CMAKE_BINARY_DIR} +) + +target_link_libraries(datatypestest PRIVATE + gmock_main + gmock + gtest + datatypes +) + +add_test(NAME datatypestest COMMAND datatypestest) + +set_tests_properties(datatypestest PROPERTIES + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" +) diff --git a/tests/datatypestest.cpp b/tests/datatypestest.cpp new file mode 100644 index 0000000..ed1d9c6 --- /dev/null +++ b/tests/datatypestest.cpp @@ -0,0 +1,150 @@ +/***************************************************************************** + * 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 +#include + +#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()); +} + -- libgit2 0.21.4