Skip to content

Commit

Permalink
Added test files and new workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
federicorossifr committed Aug 5, 2023
1 parent ab8d914 commit 30ccb75
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
41 changes: 31 additions & 10 deletions examples/99_debugger/99_debugger.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
//
// Created by Federico Rossi on 29/07/23.
//

#include "posit.h"
#include <cstdint>

float convert32(const float x) {
posit::Posit<int32_t, 32, 2, uint32_t, posit::PositSpec::WithNan> y(x);
const uint32_t z = *(uint32_t*)&y;
std::cout << z << std::endl;
return *(posit::Posit<int32_t, 32, 2, uint32_t, posit::PositSpec::WithNan>*)&z;
}

float convert16(const float x) {
posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithNan> y(x);
const int16_t z = *(int16_t*)&y;
std::cout << z << std::endl;
return *(posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithNan>*)&z;
}

typedef float (*F)(const float);

template <float (*F)(float)>
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<int32_t, 32, 2, uint32_t, posit::PositSpec::WithNan> 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);
}
27 changes: 27 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
22 changes: 22 additions & 0 deletions tests/test_init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <posit.h>

TEST_CASE("Initialize with deep initalization", "[short]") {
using P = posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithInfs>;
P a = P::from_sraw(1 << 14);
REQUIRE(a.v == (1<<14));
}

TEST_CASE("Initialize with float conversion", "[short]") {
using P = posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithInfs>;
P a(1.0f);
REQUIRE(a.v == (1<<14));
}

TEST_CASE("Initialize with integer conversion", "[short]") {
using P = posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithInfs>;
P a(1);
REQUIRE(a.v == (1<<14));
}

33 changes: 33 additions & 0 deletions tests/test_subnormals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <posit.h>

using P = posit::Posit<int32_t, 32, 2, uint32_t, posit::PositSpec::WithNan>;
using P16 = posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithNan>;

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);
}

0 comments on commit 30ccb75

Please sign in to comment.