common_value.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. }
  86. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_5)
  87. {
  88. struct S {
  89. std::string to_string() const { return "s"; }
  90. bool operator==(const S& /* other */) const { return true; }
  91. } s;
  92. artis::common::event::Value value{s};
  93. BOOST_REQUIRE_EQUAL(value.to_string(), "s");
  94. }
  95. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_6)
  96. {
  97. struct S {
  98. std::string to_string() const { return "s"; }
  99. bool operator==(const S& /* other */) const { return true; }
  100. };
  101. std::vector<S> vs{S(), S(), S(), S()};
  102. artis::common::event::Value value{vs};
  103. BOOST_REQUIRE_EQUAL(value.to_string(), "s s s s ");
  104. }
  105. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_7)
  106. {
  107. artis::common::event::Value int_value{1};
  108. artis::common::event::Value unsigned_int_value{(unsigned int) 1};
  109. artis::common::event::Value double_value{0.5};
  110. artis::common::event::Value bool_value{true};
  111. artis::common::event::Value char_value{'a'};
  112. int a;
  113. unsigned int b;
  114. double c;
  115. bool d;
  116. char e;
  117. int_value(a);
  118. unsigned_int_value(b);
  119. double_value(c);
  120. bool_value(d);
  121. char_value(e);
  122. BOOST_REQUIRE_EQUAL(a, 1);
  123. BOOST_REQUIRE_EQUAL(b, 1);
  124. BOOST_REQUIRE_EQUAL(c, 0.5);
  125. BOOST_REQUIRE_EQUAL(d, true);
  126. BOOST_REQUIRE_EQUAL(e, 'a');
  127. }
  128. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_8)
  129. {
  130. std::vector<int> int_vector{1,2,3};
  131. artis::common::event::Value int_values{int_vector};
  132. std::vector<unsigned int> unsigned_int_vector{1, 2, 3};
  133. artis::common::event::Value unsigned_int_values{unsigned_int_vector};
  134. std::vector<double> double_vector{0.5, 0.6, 1.2};
  135. artis::common::event::Value double_values{double_vector};
  136. std::vector<bool> bool_vector{true, false, true, false};
  137. artis::common::event::Value bool_values{bool_vector};
  138. std::vector<char> char_vector{'a', 'b', 'c'};
  139. artis::common::event::Value char_values{char_vector};
  140. std::vector<int> va;
  141. std::vector<unsigned int> vb;
  142. std::vector<double> vc;
  143. std::vector<bool> vd;
  144. std::vector<char> ve;
  145. int_values(va);
  146. unsigned_int_values(vb);
  147. double_values(vc);
  148. bool_values(vd);
  149. char_values(ve);
  150. BOOST_CHECK(std::equal(va.cbegin(), va.cend(), int_vector.cbegin()));
  151. BOOST_CHECK(std::equal(vb.cbegin(), vb.cend(), unsigned_int_vector.cbegin()));
  152. BOOST_CHECK(std::equal(vc.cbegin(), vc.cend(), double_vector.cbegin()));
  153. BOOST_CHECK(std::equal(vd.cbegin(), vd.cend(), bool_vector.cbegin()));
  154. BOOST_CHECK(std::equal(ve.cbegin(), ve.cend(), char_vector.cbegin()));
  155. }
  156. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_9)
  157. {
  158. struct S {
  159. std::string to_string() const { return "s"; }
  160. bool operator==(const S& /* other */) const { return true; }
  161. };
  162. S s;
  163. artis::common::event::Value value{s};
  164. S s2;
  165. value(s2);
  166. BOOST_CHECK(s == s2);
  167. }
  168. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_10)
  169. {
  170. struct S {
  171. std::string to_string() const { return "s"; }
  172. bool operator==(const S& /* other */) const { return true; }
  173. };
  174. std::vector<S> vs;
  175. artis::common::event::Value value{vs};
  176. std::vector<S> vs2;
  177. value(vs2);
  178. BOOST_CHECK(std::equal(vs.cbegin(), vs.cend(), vs2.cbegin()));
  179. }
  180. BOOST_AUTO_TEST_CASE(Common_Value_TestCase_11)
  181. {
  182. std::string s{"artis"};
  183. artis::common::event::Value value{s};
  184. std::string s2;
  185. value(s2);
  186. BOOST_REQUIRE_EQUAL(value.to_string(), "artis");
  187. BOOST_CHECK(s == s2);
  188. }