souffle  2.0.2-371-g6315b36
ram_type_conversion_test.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020, The Souffle Developers. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file ram_type_conversion_test.cpp
12  *
13  * Tests souffle's type conversions.
14  *
15  ***********************************************************************/
16 
17 #include "tests/test.h"
18 
19 #include "souffle/RamTypes.h"
20 #include <random>
21 #include <string>
22 
23 namespace souffle::ram::test {
24 
25 #define NUMBER_OF_CONVERSION_TESTS 100
26 
27 TEST(randomConversions, RamSigned) {
28  std::mt19937 randomGenerator(3);
29  std::uniform_int_distribution<RamDomain> dist(-100, 100);
30 
31  for (int i = 0; i < NUMBER_OF_CONVERSION_TESTS; ++i) {
32  RamDomain randomNumber = dist(randomGenerator);
33 
34  // Test float casting
35  EXPECT_EQ(randomNumber, ramBitCast(ramBitCast<RamFloat>(randomNumber)));
36 
37  // Test signed casting
38  EXPECT_EQ(randomNumber, ramBitCast(ramBitCast<RamUnsigned>(randomNumber)));
39  }
40 }
41 
42 TEST(randomConversions, RamUnsigned) {
43  std::mt19937 randomGenerator(3);
44  std::uniform_int_distribution<RamUnsigned> dist(0, 1000);
45 
46  for (int i = 0; i < NUMBER_OF_CONVERSION_TESTS; ++i) {
47  RamUnsigned randomNumber = dist(randomGenerator);
48 
49  // Test float casting
50  EXPECT_EQ(randomNumber, ramBitCast<RamUnsigned>(ramBitCast<RamFloat>(randomNumber)));
51 
52  // Test signed casting
53  EXPECT_EQ(randomNumber, ramBitCast<RamUnsigned>(ramBitCast<RamDomain>(randomNumber)));
54  }
55 }
56 
57 TEST(randomConversions, RamFloat) {
58  std::mt19937 randomGenerator(3);
59  std::uniform_real_distribution<RamFloat> dist(-100.0, 100.0);
60 
61  for (int i = 0; i < NUMBER_OF_CONVERSION_TESTS; ++i) {
62  RamFloat randomNumber = dist(randomGenerator);
63 
64  // Test signed casting
65  EXPECT_EQ(randomNumber, ramBitCast<RamFloat>(ramBitCast<RamDomain>(randomNumber)));
66 
67  // Test unsigned casting
68  EXPECT_EQ(randomNumber, ramBitCast<RamFloat>(ramBitCast<RamUnsigned>(randomNumber)));
69  }
70 }
71 
72 } // namespace souffle::ram::test
souffle::RamUnsigned
uint32_t RamUnsigned
Definition: RamTypes.h:58
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: test.h:191
souffle::RamFloat
float RamFloat
Definition: RamTypes.h:60
i
size_t i
Definition: json11.h:663
NUMBER_OF_CONVERSION_TESTS
#define NUMBER_OF_CONVERSION_TESTS
Definition: ram_type_conversion_test.cpp:31
test.h
souffle::ram::test::TEST
TEST(True, CloneAndEquals)
Definition: ram_condition_equal_clone_test.cpp:54
RamTypes.h
souffle::ramBitCast
To ramBitCast(From source)
In C++20 there will be a new way to cast between types by reinterpreting bits (std::bit_cast),...
Definition: RamTypes.h:87
souffle::RamSigned
RamDomain RamSigned
Definition: RamTypes.h:57
souffle::ram::test
Definition: ram_condition_equal_clone_test.cpp:46