common_value.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @file tests/common_value.cpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2022 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #include "artis-star/common/event/Value.hpp"
  26. #define BOOST_TEST_MODULE Common_Value_Tests
  27. #include <boost/test/unit_test.hpp>
  28. /*************************************************
  29. * Tests
  30. *************************************************/
  31. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_1)
  32. {
  33. artis::common::event::Value first_value{1};
  34. artis::common::event::Value second_value{1};
  35. BOOST_CHECK(first_value == second_value);
  36. }
  37. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_2)
  38. {
  39. artis::common::event::Value int_value{1};
  40. artis::common::event::Value unsigned_int_value{(unsigned int) 1};
  41. artis::common::event::Value double_value{0.5};
  42. artis::common::event::Value bool_value{true};
  43. artis::common::event::Value char_value{'a'};
  44. BOOST_REQUIRE_EQUAL(int_value.to_string(), "1");
  45. BOOST_REQUIRE_EQUAL(unsigned_int_value.to_string(), "1");
  46. BOOST_REQUIRE_EQUAL(double_value.to_string(), "0.5");
  47. BOOST_REQUIRE_EQUAL(bool_value.to_string(), "true");
  48. BOOST_REQUIRE_EQUAL(char_value.to_string(), "a");
  49. }
  50. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_3)
  51. {
  52. int int_values[] = {1, 2, 3};
  53. artis::common::event::Value int_value{int_values, 3};
  54. unsigned int unsigned_int_values[] = {1, 2, 3};
  55. artis::common::event::Value unsigned_int_value{unsigned_int_values, 3};
  56. double double_values[] = {0.5, 0.6, 1.2};
  57. artis::common::event::Value double_value{double_values, 3};
  58. bool bool_values[] = {true, false, true, false};
  59. artis::common::event::Value bool_value{bool_values, 4};
  60. char char_values[] = {'a', 'b', 'c'};
  61. artis::common::event::Value char_value{char_values};
  62. BOOST_REQUIRE_EQUAL(int_value.to_string(), "1 2 3 ");
  63. BOOST_REQUIRE_EQUAL(unsigned_int_value.to_string(), "1 2 3 ");
  64. BOOST_REQUIRE_EQUAL(double_value.to_string(), "0.5 0.6 1.2 ");
  65. BOOST_REQUIRE_EQUAL(bool_value.to_string(), "true false true false ");
  66. BOOST_REQUIRE_EQUAL(char_value.to_string(), "abc");
  67. }
  68. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_4)
  69. {
  70. std::vector<int> int_vector{1,2,3};
  71. artis::common::event::Value int_values{int_vector};
  72. std::vector<unsigned int> unsigned_int_vector{1, 2, 3};
  73. artis::common::event::Value unsigned_int_values{unsigned_int_vector};
  74. std::vector<double> double_vector{0.5, 0.6, 1.2};
  75. artis::common::event::Value double_values{double_vector};
  76. std::vector<bool> bool_vector{true, false, true, false};
  77. artis::common::event::Value bool_values{bool_vector};
  78. std::vector<char> char_vector{'a', 'b', 'c'};
  79. artis::common::event::Value char_values{char_vector};
  80. BOOST_REQUIRE_EQUAL(int_values.to_string(), "1 2 3 ");
  81. BOOST_REQUIRE_EQUAL(unsigned_int_values.to_string(), "1 2 3 ");
  82. BOOST_REQUIRE_EQUAL(double_values.to_string(), "0.5 0.6 1.2 ");
  83. BOOST_REQUIRE_EQUAL(bool_values.to_string(), "true false true false ");
  84. BOOST_REQUIRE_EQUAL(char_values.to_string(), "a b c ");
  85. BOOST_REQUIRE_EQUAL(int_values.size(), 3);
  86. BOOST_REQUIRE_EQUAL(unsigned_int_values.size(), 3);
  87. BOOST_REQUIRE_EQUAL(double_values.size(), 3);
  88. BOOST_REQUIRE_EQUAL(bool_values.size(), 4);
  89. BOOST_REQUIRE_EQUAL(char_values.size(), 3);
  90. }
  91. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_5)
  92. {
  93. struct S {
  94. std::string to_string() const { return "s"; }
  95. bool operator==(const S& /* other */) const { return true; }
  96. } s;
  97. artis::common::event::Value value{s};
  98. BOOST_REQUIRE_EQUAL(value.to_string(), "s");
  99. BOOST_REQUIRE_EQUAL(value.size(), 1);
  100. }
  101. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_6)
  102. {
  103. struct S {
  104. std::string to_string() const { return "s"; }
  105. bool operator==(const S& /* other */) const { return true; }
  106. };
  107. std::vector<S> vs{S(), S(), S(), S()};
  108. artis::common::event::Value value{vs};
  109. BOOST_REQUIRE_EQUAL(value.to_string(), "s s s s ");
  110. BOOST_REQUIRE_EQUAL(value.size(), 4);
  111. }
  112. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_7)
  113. {
  114. artis::common::event::Value int_value{1};
  115. artis::common::event::Value unsigned_int_value{(unsigned int) 1};
  116. artis::common::event::Value double_value{0.5};
  117. artis::common::event::Value bool_value{true};
  118. artis::common::event::Value char_value{'a'};
  119. int a;
  120. unsigned int b;
  121. double c;
  122. bool d;
  123. char e;
  124. int_value(a);
  125. unsigned_int_value(b);
  126. double_value(c);
  127. bool_value(d);
  128. char_value(e);
  129. BOOST_REQUIRE_EQUAL(a, 1);
  130. BOOST_REQUIRE_EQUAL(b, 1);
  131. BOOST_REQUIRE_EQUAL(c, 0.5);
  132. BOOST_REQUIRE_EQUAL(d, true);
  133. BOOST_REQUIRE_EQUAL(e, 'a');
  134. }
  135. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_8)
  136. {
  137. std::vector<int> int_vector{1,2,3};
  138. artis::common::event::Value int_values{int_vector};
  139. std::vector<unsigned int> unsigned_int_vector{1, 2, 3};
  140. artis::common::event::Value unsigned_int_values{unsigned_int_vector};
  141. std::vector<double> double_vector{0.5, 0.6, 1.2};
  142. artis::common::event::Value double_values{double_vector};
  143. std::vector<bool> bool_vector{true, false, true, false};
  144. artis::common::event::Value bool_values{bool_vector};
  145. std::vector<char> char_vector{'a', 'b', 'c'};
  146. artis::common::event::Value char_values{char_vector};
  147. std::vector<int> va;
  148. std::vector<unsigned int> vb;
  149. std::vector<double> vc;
  150. std::vector<bool> vd;
  151. std::vector<char> ve;
  152. int_values(va);
  153. unsigned_int_values(vb);
  154. double_values(vc);
  155. bool_values(vd);
  156. char_values(ve);
  157. BOOST_CHECK(std::equal(va.cbegin(), va.cend(), int_vector.cbegin()));
  158. BOOST_CHECK(std::equal(vb.cbegin(), vb.cend(), unsigned_int_vector.cbegin()));
  159. BOOST_CHECK(std::equal(vc.cbegin(), vc.cend(), double_vector.cbegin()));
  160. BOOST_CHECK(std::equal(vd.cbegin(), vd.cend(), bool_vector.cbegin()));
  161. BOOST_CHECK(std::equal(ve.cbegin(), ve.cend(), char_vector.cbegin()));
  162. }
  163. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_9)
  164. {
  165. struct S {
  166. std::string to_string() const { return "s"; }
  167. bool operator==(const S& /* other */) const { return true; }
  168. };
  169. S s;
  170. artis::common::event::Value value{s};
  171. S s2;
  172. value(s2);
  173. BOOST_CHECK(s == s2);
  174. }
  175. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_10)
  176. {
  177. struct S {
  178. std::string to_string() const { return "s"; }
  179. bool operator==(const S& /* other */) const { return true; }
  180. };
  181. std::vector<S> vs;
  182. artis::common::event::Value value{vs};
  183. std::vector<S> vs2;
  184. value(vs2);
  185. BOOST_CHECK(std::equal(vs.cbegin(), vs.cend(), vs2.cbegin()));
  186. }
  187. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_11)
  188. {
  189. std::string s{"artis"};
  190. artis::common::event::Value value{s};
  191. std::string s2;
  192. value(s2);
  193. BOOST_REQUIRE_EQUAL(value.to_string(), "artis");
  194. BOOST_CHECK(s == s2);
  195. }