diff --git a/meson.build b/meson.build index 3b97bc9..08c324b 100644 --- a/meson.build +++ b/meson.build @@ -31,4 +31,5 @@ cpp = meson.get_compiler('cpp') subdir('assets/static') subdir('src') +subdir('tests') diff --git a/readme.md b/readme.md index e69de29..cca6659 100644 --- a/readme.md +++ b/readme.md @@ -0,0 +1,5 @@ +# libconstants + +libconstants is the authoritative source for physical constants for the SERiF project. + +This has been broken out of the main serif project to allow for more modularity diff --git a/subprojects/gtest.wrap b/subprojects/gtest.wrap new file mode 100644 index 0000000..7b1e69e --- /dev/null +++ b/subprojects/gtest.wrap @@ -0,0 +1,16 @@ +[wrap-file] +directory = googletest-1.15.2 +source_url = https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz +source_filename = gtest-1.15.2.tar.gz +source_hash = 7b42b4d6ed48810c5362c265a17faebe90dc2373c885e5216439d37927f02926 +patch_filename = gtest_1.15.2-2_patch.zip +patch_url = https://wrapdb.mesonbuild.com/v2/gtest_1.15.2-2/get_patch +patch_hash = 641a16b33c96cd32a593537bc30eb7d853c5cc361fa1ee96884f0e2fca21e2d3 +source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/gtest_1.15.2-2/gtest-1.15.2.tar.gz +wrapdb_version = 1.15.2-2 + +[provide] +gtest = gtest_dep +gtest_main = gtest_main_dep +gmock = gmock_dep +gmock_main = gmock_main_dep diff --git a/tests/constants/constTest.cpp b/tests/constants/constTest.cpp new file mode 100644 index 0000000..b458339 --- /dev/null +++ b/tests/constants/constTest.cpp @@ -0,0 +1,109 @@ +#include +#include "const.h" +#include +#include +#include +#include +#include + +/** + * @file constTest.cpp + * @brief Unit tests for the const class. + */ + +/** + * @brief Test suite for the const class. + */ +class constTest : public ::testing::Test { +protected: + void SetUp() override { + serif::constant::Constants::getInstance(); + } +}; + +/** + * @test Verify default constructor initializes correctly. + */ +TEST_F(constTest, DefaultConstructor) { + EXPECT_NO_THROW(serif::constant::Constants::getInstance()); +} + +/** + * @test Verify constructor initializes with provided data and metadata. + */ +TEST_F(constTest, isLoaded) { + + EXPECT_NO_THROW(serif::constant::Constants::getInstance().isLoaded()); +} + +/** + * @test Verify get method returns the correct constant. + */ +TEST_F(constTest, GetMethod) { + serif::constant::Constants& obj = serif::constant::Constants::getInstance(); + EXPECT_DOUBLE_EQ(obj.get("c").value, 2.99792458e10); + EXPECT_EQ(obj.get("c").unit, "cm / s"); + EXPECT_DOUBLE_EQ(obj.get("c").uncertainty, 0.0); + EXPECT_EQ(obj.get("c").reference, "CODATA2022"); +} + +/** + * @test Verify [] opperators returns the correct constant. + */ +TEST_F(constTest, SubscriptOperator) { + serif::constant::Constants& obj = serif::constant::Constants::getInstance(); + EXPECT_DOUBLE_EQ(obj["c"].value, 2.99792458e10); + EXPECT_EQ(obj["c"].unit, "cm / s"); + EXPECT_DOUBLE_EQ(obj["c"].uncertainty, 0.0); + EXPECT_EQ(obj["c"].reference, "CODATA2022"); +} + +/** + * @test Verify that the has method returns the correct values + */ +TEST_F(constTest, HasMethod) { + serif::constant::Constants& obj = serif::constant::Constants::getInstance(); + + EXPECT_TRUE(obj.has("c")); + EXPECT_FALSE(obj.has("c4")); + EXPECT_TRUE(obj.has("hbar")); +} + +TEST_F(constTest, KeysMethod) { + serif::constant::Constants& obj = serif::constant::Constants::getInstance(); + std::set checkKeys; + checkKeys.insert("c"); + checkKeys.insert("wienK"); + checkKeys.insert("hbar"); + checkKeys.insert("g_h"); + checkKeys.insert("R_sun"); + + std::set keys = obj.keys(); + + for (const auto& key : checkKeys) { + bool found = keys.find(key) != keys.end(); + EXPECT_TRUE(found); + } + + std::set checkBadKeys; + checkBadKeys.insert("c4"); + checkBadKeys.insert("wienK4"); + checkBadKeys.insert("hbar4"); + checkBadKeys.insert("g_h4"); + checkBadKeys.insert("R_sun4"); + + for (const auto& key : checkBadKeys) { + bool found = keys.find(key) != keys.end(); + EXPECT_FALSE(found); + } +} + +TEST_F(constTest, StreamOperator) { + serif::constant::Constants& obj = serif::constant::Constants::getInstance(); + std::ostringstream os; + + os << obj.get("c"); + std::string expected = "\n"; + + EXPECT_EQ(os.str(), expected); +} \ No newline at end of file diff --git a/tests/constants/meson.build b/tests/constants/meson.build new file mode 100644 index 0000000..53ec814 --- /dev/null +++ b/tests/constants/meson.build @@ -0,0 +1,22 @@ +# Test files for const +test_sources = [ + 'constTest.cpp', +] + +foreach test_file : test_sources + exe_name = test_file.split('.')[0] + message('Building test: ' + exe_name) + + # Create an executable target for each test + test_exe = executable( + exe_name, + test_file, + dependencies: [gtest_dep, const_dep, gtest_main], + include_directories: include_directories('../../src/constants/public'), + link_with: libconst, # Link the dobj library + install_rpath: '@loader_path/../../src' # Ensure runtime library path resolves correctly + ) + + # Add the executable as a test + test(exe_name, test_exe) +endforeach diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 0000000..fc313c9 --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,7 @@ +# Google Test dependency +gtest_dep = dependency('gtest', main: true, required : true) +gtest_main = dependency('gtest_main', required: true) +gtest_nomain_dep = dependency('gtest', main: false, required : true) + +# Subdirectories for unit and integration tests +subdir('constants')