1
2 #include <iostream>
3 #include <string>
4 #include <cstring>
5 #include <gtest/gtest.h>
6
7 #undef A_LIB_NAMESPACE
8 #define A_LIB_NAMESPACE Davix
9
10
11 #include <alibxx/alibxx.hpp>
12
13
14
15 using namespace std;
16
17 struct DummyStruct{
18 std::string dude;
19 };
20
21
22 // instanciate and play with gates
TEST(ALibxx,StringToULong)23 TEST(ALibxx, StringToULong){
24 using namespace Davix;
25 std::string str_ulong("123456");
26 toType<unsigned long, std::string> conv;
27
28 unsigned long ul = conv(str_ulong);
29
30 ASSERT_EQ(ul, 123456);
31
32 str_ulong = "0";
33 ul= conv(str_ulong);
34 ASSERT_EQ(ul, 0);
35
36 try{
37 std::string str2 = "random string";
38 Davix::toType<unsigned long, std::string>()(str2);
39 FAIL();
40 }catch(TypeConvException & e){
41 // silent
42 }
43
44
45 try{
46 std::string str2 = "-15";
47 Davix::toType<unsigned long, std::string>()(str2);
48 FAIL();
49 }catch(TypeConvException & e){
50 // silent
51 }
52
53 unsigned long long super_long = std::numeric_limits<unsigned long long>::max();
54 std::ostringstream ss;
55 ss << super_long;
56 ss << super_long; // overflow
57 try{
58 ul =toType<unsigned long, std::string>()(ss.str());
59 FAIL();
60 }catch(...){
61 // silent
62 }
63
64 }
65
66
67 // instanciate and play with gates
TEST(ALibxx,StringToLong)68 TEST(ALibxx, StringToLong){
69 using namespace Davix;
70 std::string str_long("123456");
71 toType<long, std::string> conv;
72
73 long l = conv(str_long);
74
75 ASSERT_EQ(l, 123456);
76
77 str_long = "0";
78 l= conv(str_long);
79 ASSERT_EQ(l, 0);
80
81 try{
82 std::string str2 = "random string";
83 Davix::toType<long, std::string>()(str2);
84 FAIL();
85 }catch(TypeConvException & e){
86 // silent
87 }
88
89
90 str_long = "-9865743";
91 l = conv(str_long);
92 ASSERT_EQ(l, -9865743);
93
94 }
95
96
97
98 // instanciate and play with gates
TEST(ALibxx,StringToInt)99 TEST(ALibxx, StringToInt){
100 using namespace Davix;
101 std::string str_int("123456");
102 toType<int, std::string> conv;
103
104 int l = conv(str_int);
105
106 ASSERT_EQ(l, 123456);
107
108 str_int = "0";
109 l= conv(str_int);
110 ASSERT_EQ(l, 0);
111
112 try{
113 std::string str2 = "random string";
114 toType<int, std::string>()(str2);
115 FAIL();
116 }catch(TypeConvException & e){
117 // silent
118 }
119
120
121 str_int = "-9865743";
122 l = conv(str_int);
123 ASSERT_EQ(l, -9865743);
124
125
126 long long super_long = std::numeric_limits<long long>::max();
127 std::ostringstream ss;
128 ss << super_long;
129 ss << super_long; // overflow
130 try{
131 l =toType<int, std::string>()(ss.str());
132 FAIL();
133 }catch(...){
134 // silent
135 }
136 }
137