diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 2eecef9..6588a23 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -33,4 +33,7 @@ jobs: # Build your program with the given configuration run: cmake --build ${{github.workspace}}/buildc --config ${{env.BUILD_TYPE}} --target examples - + - name: Build and run Tests + run: > + cmake --build ${{github.workspace}}/buildc; + ctest --verbose; diff --git a/examples/99_debugger/99_debugger.cpp b/examples/99_debugger/99_debugger.cpp index 0886d6e..468e842 100644 --- a/examples/99_debugger/99_debugger.cpp +++ b/examples/99_debugger/99_debugger.cpp @@ -1,13 +1,34 @@ -// -// Created by Federico Rossi on 29/07/23. -// - #include "posit.h" -#include + +float convert32(const float x) { + posit::Posit y(x); + const uint32_t z = *(uint32_t*)&y; + std::cout << z << std::endl; + return *(posit::Posit*)&z; +} + +float convert16(const float x) { + posit::Posit y(x); + const int16_t z = *(int16_t*)&y; + std::cout << z << std::endl; + return *(posit::Posit*)&z; +} + +typedef float (*F)(const float); + +template +void calc(float x, int bits) { + std::cout << "input = " << x << ", converted back and forth with " << bits + << " bits = " << F(x) + << ", ratio = " << std::abs(F(x)/double(x)) << std::endl; +} + int main() { - uint64_t bigNum = uint64_t(INT32_MAX)*2; - std::cout << bigNum << " " << INT32_MAX << std::endl; - posit::Posit x(bigNum); - std::cout << (uint64_t)x << std::endl; - return 0; + std::cout << "Using cppposit library" << std::endl << std::endl; + + calc<&convert32>(1.75911e-33, 32); + calc<&convert32>(-1.15356e-33f, 32); + calc<&convert16>(6.40675e-27, 16); + calc<&convert16>(-8.76183e-37, 16); + calc<&convert16>(1.4013e-45, 16); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..0ef44c0 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,27 @@ +include(FetchContent) +# declare Catch2 +FetchContent_Declare(Catch2 # name of the content + GIT_REPOSITORY https://github.com/catchorg/Catch2.git # the repository + GIT_TAG v2.13.7 # the tag + ) + +# make available +FetchContent_MakeAvailable(Catch2) + + +add_executable(test_init test_init.cpp) +target_link_libraries(test_init PRIVATE Catch2::Catch2) + +add_executable(test_subnormals test_subnormals.cpp) +target_link_libraries(test_subnormals PRIVATE Catch2::Catch2) + + +add_test( + NAME test_init + COMMAND test_init --success +) + +add_test( + NAME test_subnormals + COMMAND test_subnormals --success +) \ No newline at end of file diff --git a/tests/test_init.cpp b/tests/test_init.cpp new file mode 100644 index 0000000..94af172 --- /dev/null +++ b/tests/test_init.cpp @@ -0,0 +1,22 @@ +#define CATCH_CONFIG_MAIN +#include +#include + +TEST_CASE("Initialize with deep initalization", "[short]") { + using P = posit::Posit; + P a = P::from_sraw(1 << 14); + REQUIRE(a.v == (1<<14)); +} + +TEST_CASE("Initialize with float conversion", "[short]") { + using P = posit::Posit; + P a(1.0f); + REQUIRE(a.v == (1<<14)); +} + +TEST_CASE("Initialize with integer conversion", "[short]") { + using P = posit::Posit; + P a(1); + REQUIRE(a.v == (1<<14)); +} + diff --git a/tests/test_subnormals.cpp b/tests/test_subnormals.cpp new file mode 100644 index 0000000..57f351a --- /dev/null +++ b/tests/test_subnormals.cpp @@ -0,0 +1,33 @@ +#define CATCH_CONFIG_MAIN +#include +#include + +using P = posit::Posit; +using P16 = posit::Posit; + +double convert32(const double x) { + P y(x); + const uint32_t z = *(uint32_t*)&y; + return *(P*)&z; +} + +float convert16(const float x) { + P16 y(x); + const uint16_t z = *(uint16_t*)&y; + return *(P16*)&z; +} + + +TEST_CASE("Test 32,2 minposit", "[short]") { + float a = convert32(1.75911e-33f); + float b = convert32(-1.15356e-33f); + REQUIRE(P(a).v == 7u); + REQUIRE(P(b).v == 4294967290u); +} + +TEST_CASE("Test 16,2 minposit", "[short]") { + float a = convert32(6.40675e-27f); + float b = convert32(-8.76183e-37f); + REQUIRE(P16(a).v == 1u); + REQUIRE(P16(b).v == -1u); +}