You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

497 lines
12 KiB

  1. #include "gtest/gtest.h"
  2. #include "src/storage/BitVector.h"
  3. #include "src/exceptions/InvalidArgumentException.h"
  4. #include "src/exceptions/OutOfRangeException.h"
  5. TEST(BitVectorTest, InitToZero) {
  6. storm::storage::BitVector vector(32);
  7. for (uint_fast64_t i = 0; i < 32; ++i) {
  8. ASSERT_FALSE(vector.get(i));
  9. }
  10. ASSERT_TRUE(vector.empty());
  11. ASSERT_FALSE(vector.full());
  12. }
  13. TEST(BitVectorTest, InitToOne) {
  14. storm::storage::BitVector vector(32, true);
  15. for (uint_fast64_t i = 0; i < 32; ++i) {
  16. ASSERT_TRUE(vector.get(i));
  17. }
  18. ASSERT_FALSE(vector.empty());
  19. ASSERT_TRUE(vector.full());
  20. }
  21. TEST(BitVectorTest, InitFromIterator) {
  22. std::vector<uint_fast64_t> valueVector = {0, 4, 10};
  23. storm::storage::BitVector vector(32, valueVector.begin(), valueVector.end());
  24. ASSERT_EQ(32, vector.size());
  25. for (uint_fast64_t i = 0; i < 32; ++i) {
  26. if (i == 0 || i == 4 || i == 10) {
  27. ASSERT_TRUE(vector.get(i));
  28. } else {
  29. ASSERT_FALSE(vector.get(i));
  30. }
  31. }
  32. }
  33. TEST(BitVectorTest, GetSet) {
  34. storm::storage::BitVector vector(32);
  35. for (uint_fast64_t i = 0; i < 32; ++i) {
  36. vector.set(i, i % 2 == 0);
  37. }
  38. for (uint_fast64_t i = 0; i < 32; ++i) {
  39. ASSERT_EQ(i % 2 == 0, vector.get(i));
  40. }
  41. }
  42. TEST(BitVectorTest, GetAsInt) {
  43. storm::storage::BitVector vector(77);
  44. vector.set(62);
  45. vector.set(63);
  46. vector.set(64);
  47. vector.set(65);
  48. EXPECT_EQ(1, vector.getAsInt(62, 1));
  49. EXPECT_EQ(3, vector.getAsInt(62, 2));
  50. EXPECT_EQ(7, vector.getAsInt(62, 3));
  51. EXPECT_EQ(15, vector.getAsInt(62, 4));
  52. vector.set(64, false);
  53. EXPECT_EQ(1, vector.getAsInt(62, 1));
  54. EXPECT_EQ(3, vector.getAsInt(62, 2));
  55. EXPECT_EQ(6, vector.getAsInt(62, 3));
  56. EXPECT_EQ(13, vector.getAsInt(62, 4));
  57. vector.set(61);
  58. vector.set(62, false);
  59. EXPECT_EQ(2, vector.getAsInt(61, 2));
  60. }
  61. TEST(BitVectorTest, SetFromInt) {
  62. storm::storage::BitVector vector(77);
  63. vector.setFromInt(62, 1, 1);
  64. EXPECT_TRUE(vector.get(62));
  65. EXPECT_FALSE(vector.get(63));
  66. EXPECT_FALSE(vector.get(64));
  67. EXPECT_FALSE(vector.get(65));
  68. vector.setFromInt(61, 2, 2);
  69. EXPECT_TRUE(vector.get(61));
  70. EXPECT_FALSE(vector.get(62));
  71. EXPECT_FALSE(vector.get(63));
  72. vector.setFromInt(61, 3, 5);
  73. EXPECT_TRUE(vector.get(61));
  74. EXPECT_FALSE(vector.get(62));
  75. EXPECT_TRUE(vector.get(63));
  76. vector = storm::storage::BitVector(77);
  77. vector.setFromInt(62, 4, 15);
  78. EXPECT_TRUE(vector.get(62));
  79. EXPECT_TRUE(vector.get(63));
  80. EXPECT_TRUE(vector.get(64));
  81. EXPECT_TRUE(vector.get(65));
  82. vector.setFromInt(62, 5, 17);
  83. }
  84. TEST(BitVectorTest, GetSetInt) {
  85. storm::storage::BitVector vector(77);
  86. vector.setFromInt(63, 3, 2);
  87. EXPECT_EQ(2, vector.getAsInt(63, 3));
  88. }
  89. TEST(BitVectorDeathTest, GetSetAssertion) {
  90. storm::storage::BitVector vector(32);
  91. #ifndef NDEBUG
  92. #ifdef WINDOWS
  93. EXPECT_EXIT(vector.get(32), ::testing::ExitedWithCode(0), ".*");
  94. EXPECT_EXIT(vector.set(32), ::testing::ExitedWithCode(0), ".*");
  95. #else
  96. EXPECT_DEATH_IF_SUPPORTED(vector.get(32), "");
  97. EXPECT_DEATH_IF_SUPPORTED(vector.set(32), "");
  98. #endif
  99. #else
  100. std::cerr << "WARNING: Not testing GetSetAssertions, as they are disabled in release mode." << std::endl;
  101. SUCCEED();
  102. #endif
  103. }
  104. TEST(BitVectorTest, Resize) {
  105. storm::storage::BitVector vector(32);
  106. for (uint_fast64_t i = 0; i < 32; ++i) {
  107. vector.set(i);
  108. }
  109. vector.resize(70);
  110. ASSERT_EQ(70, vector.size());
  111. ASSERT_EQ(32, vector.getNumberOfSetBits());
  112. for (uint_fast64_t i = 0; i < 32; ++i) {
  113. ASSERT_TRUE(vector.get(i));
  114. }
  115. bool result;
  116. for (uint_fast64_t i = 32; i < 70; ++i) {
  117. result = true;
  118. ASSERT_NO_THROW(result = vector.get(i));
  119. ASSERT_FALSE(result);
  120. }
  121. vector.resize(72, true);
  122. ASSERT_EQ(72, vector.size());
  123. ASSERT_EQ(34, vector.getNumberOfSetBits());
  124. for (uint_fast64_t i = 0; i < 32; ++i) {
  125. ASSERT_TRUE(vector.get(i));
  126. }
  127. for (uint_fast64_t i = 32; i < 70; ++i) {
  128. result = true;
  129. ASSERT_NO_THROW(result = vector.get(i));
  130. ASSERT_FALSE(result);
  131. }
  132. for (uint_fast64_t i = 70; i < 72; ++i) {
  133. ASSERT_TRUE(vector.get(i));
  134. }
  135. vector.resize(16, 0);
  136. ASSERT_EQ(16, vector.size());
  137. ASSERT_EQ(16, vector.getNumberOfSetBits());
  138. for (uint_fast64_t i = 0; i < 16; ++i) {
  139. ASSERT_TRUE(vector.get(i));
  140. }
  141. vector.resize(65, 1);
  142. ASSERT_EQ(65, vector.size());
  143. ASSERT_TRUE(vector.full());
  144. }
  145. TEST(BitVectorTest, OperatorAnd) {
  146. storm::storage::BitVector vector1(32);
  147. storm::storage::BitVector vector2(32);
  148. for (int i = 0; i < 32; ++i) {
  149. vector1.set(i, i % 2 == 0);
  150. vector2.set(i, i % 2 == 1);
  151. }
  152. vector1.set(31);
  153. vector2.set(31);
  154. storm::storage::BitVector andResult = vector1 & vector2;
  155. for (uint_fast64_t i = 0; i < 31; ++i) {
  156. ASSERT_FALSE(andResult.get(i));
  157. }
  158. ASSERT_TRUE(andResult.get(31));
  159. }
  160. TEST(BitVectorTest, OperatorAndEqual) {
  161. storm::storage::BitVector vector1(32);
  162. storm::storage::BitVector vector2(32);
  163. for (int i = 0; i < 32; ++i) {
  164. vector1.set(i, i % 2 == 0);
  165. vector2.set(i, i % 2 == 1);
  166. }
  167. vector1.set(31);
  168. vector2.set(31);
  169. vector1 &= vector2;
  170. for (uint_fast64_t i = 0; i < 31; ++i) {
  171. ASSERT_FALSE(vector1.get(i));
  172. }
  173. ASSERT_TRUE(vector1.get(31));
  174. }
  175. TEST(BitVectorTest, OperatorOr) {
  176. storm::storage::BitVector vector1(32);
  177. storm::storage::BitVector vector2(32);
  178. for (uint_fast64_t i = 0; i < 32; ++i) {
  179. vector1.set(i, i % 2 == 0);
  180. vector2.set(i, i % 2 == 1);
  181. }
  182. vector1.set(31, false);
  183. vector2.set(31, false);
  184. storm::storage::BitVector orResult = vector1 | vector2;
  185. for (uint_fast64_t i = 0; i < 31; ++i) {
  186. ASSERT_TRUE(orResult.get(i));
  187. }
  188. ASSERT_FALSE(orResult.get(31));
  189. }
  190. TEST(BitVectorTest, OperatorOrEqual) {
  191. storm::storage::BitVector vector1(32);
  192. storm::storage::BitVector vector2(32);
  193. for (uint_fast64_t i = 0; i < 32; ++i) {
  194. vector1.set(i, i % 2 == 0);
  195. vector2.set(i, i % 2 == 1);
  196. }
  197. vector1.set(31, false);
  198. vector2.set(31, false);
  199. vector1 |= vector2;
  200. for (uint_fast64_t i = 0; i < 31; ++i) {
  201. ASSERT_TRUE(vector1.get(i));
  202. }
  203. ASSERT_FALSE(vector1.get(31));
  204. }
  205. TEST(BitVectorTest, OperatorXor) {
  206. storm::storage::BitVector vector1(32);
  207. storm::storage::BitVector vector2(32);
  208. for (uint_fast64_t i = 0; i < 32; ++i) {
  209. vector1.set(i);
  210. vector2.set(i, i % 2 == 1);
  211. }
  212. storm::storage::BitVector vector3 = vector1 ^ vector2;
  213. storm::storage::BitVector vector4 = ~vector2;
  214. storm::storage::BitVector vector5 = vector1 ^ vector1;
  215. for (uint_fast64_t i = 0; i < 32; ++i) {
  216. ASSERT_EQ(vector3.get(i), vector4.get(i));
  217. ASSERT_FALSE(vector5.get(i));
  218. }
  219. }
  220. TEST(BitVectorTest, OperatorModulo) {
  221. storm::storage::BitVector vector1(32);
  222. storm::storage::BitVector vector2(32);
  223. for (uint_fast64_t i = 0; i < 15; ++i) {
  224. vector2.set(i, i % 2 == 0);
  225. }
  226. vector1.set(2);
  227. vector1.set(5);
  228. vector1.set(6);
  229. storm::storage::BitVector moduloResult = vector1 % vector2;
  230. ASSERT_EQ(moduloResult.size(), 8);
  231. ASSERT_EQ(moduloResult.getNumberOfSetBits(), 2);
  232. for (uint_fast64_t i = 0; i < 8; ++i) {
  233. if (i == 1 || i == 3) {
  234. ASSERT_TRUE(moduloResult.get(i));
  235. } else {
  236. ASSERT_FALSE(moduloResult.get(i));
  237. }
  238. }
  239. storm::storage::BitVector vector3(31);
  240. for (uint_fast64_t i = 0; i < 15; ++i) {
  241. vector3.set(i, i % 2 == 0);
  242. }
  243. #ifndef NDEBUG
  244. #ifdef WINDOWS
  245. EXPECT_EXIT(vector1 % vector3, ::testing::ExitedWithCode(0), ".*");
  246. #else
  247. EXPECT_DEATH_IF_SUPPORTED(vector1 % vector3, "");
  248. #endif
  249. #else
  250. std::cerr << "WARNING: Not testing OperatorModulo size check, as assertions are disabled in release mode." << std::endl;
  251. SUCCEED();
  252. #endif
  253. }
  254. TEST(BitVectorTest, OperatorNot) {
  255. storm::storage::BitVector vector1(32);
  256. storm::storage::BitVector vector2(32);
  257. for (uint_fast64_t i = 0; i < 32; ++i) {
  258. vector1.set(i, i % 2 == 0);
  259. vector2.set(i, i % 2 == 1);
  260. }
  261. storm::storage::BitVector notResult = ~vector2;
  262. for (uint_fast64_t i = 0; i < 32; ++i) {
  263. ASSERT_EQ(vector1.get(i), notResult.get(i));
  264. }
  265. }
  266. TEST(BitVectorTest, Complement) {
  267. storm::storage::BitVector vector1(32);
  268. storm::storage::BitVector vector2(32);
  269. for (uint_fast64_t i = 0; i < 32; ++i) {
  270. vector1.set(i, i % 2 == 0);
  271. vector2.set(i, i % 2 == 1);
  272. }
  273. vector2.complement();
  274. for (uint_fast64_t i = 0; i < 32; ++i) {
  275. ASSERT_EQ(vector1.get(i), vector2.get(i));
  276. }
  277. }
  278. TEST(BitVectorTest, Implies) {
  279. storm::storage::BitVector vector1(32);
  280. storm::storage::BitVector vector2(32, true);
  281. for (uint_fast64_t i = 0; i < 32; ++i) {
  282. vector1.set(i, i % 2 == 0);
  283. }
  284. vector2.set(31, false);
  285. vector2.set(30, false);
  286. storm::storage::BitVector impliesResult = vector1.implies(vector2);
  287. for (uint_fast64_t i = 0; i < 30; ++i) {
  288. ASSERT_TRUE(impliesResult.get(i));
  289. }
  290. ASSERT_FALSE(impliesResult.get(30));
  291. ASSERT_TRUE(impliesResult.get(31));
  292. }
  293. TEST(BitVectorTest, Subset) {
  294. storm::storage::BitVector vector1(32);
  295. storm::storage::BitVector vector2(32, true);
  296. for (uint_fast64_t i = 0; i < 32; ++i) {
  297. vector1.set(i, i % 2 == 0);
  298. }
  299. ASSERT_TRUE(vector1.isSubsetOf(vector2));
  300. vector2.set(16, false);
  301. ASSERT_FALSE(vector1.isSubsetOf(vector2));
  302. }
  303. TEST(BitVectorTest, Disjoint) {
  304. storm::storage::BitVector vector1(32);
  305. storm::storage::BitVector vector2(32);
  306. for (uint_fast64_t i = 0; i < 32; ++i) {
  307. vector1.set(i, i % 2 == 0);
  308. vector2.set(i, i % 2 == 1);
  309. }
  310. ASSERT_TRUE(vector1.isDisjointFrom(vector2));
  311. vector2.set(16, true);
  312. ASSERT_FALSE(vector1.isDisjointFrom(vector2));
  313. }
  314. TEST(BitVectorTest, Empty) {
  315. storm::storage::BitVector vector(32);
  316. ASSERT_TRUE(vector.empty());
  317. vector.set(17, true);
  318. ASSERT_FALSE(vector.empty());
  319. vector.set(17, false);
  320. vector.set(18, false);
  321. ASSERT_TRUE(vector.empty());
  322. }
  323. TEST(BitVectorTest, Full) {
  324. storm::storage::BitVector vector(32, true);
  325. ASSERT_TRUE(vector.full());
  326. vector.set(17, false);
  327. ASSERT_FALSE(vector.full());
  328. vector.set(17, true);
  329. vector.set(18, true);
  330. ASSERT_TRUE(vector.full());
  331. }
  332. TEST(BitVectorTest, NumberOfSetBits) {
  333. storm::storage::BitVector vector(32);
  334. for (uint_fast64_t i = 0; i < 32; ++i) {
  335. vector.set(i, i % 2 == 0);
  336. }
  337. ASSERT_EQ(16, vector.getNumberOfSetBits());
  338. }
  339. TEST(BitVectorTest, NumberOfSetBitsBeforeIndex) {
  340. storm::storage::BitVector vector(32);
  341. for (uint_fast64_t i = 0; i < 32; ++i) {
  342. vector.set(i, i % 2 == 0);
  343. }
  344. ASSERT_EQ(7, vector.getNumberOfSetBitsBeforeIndex(14));
  345. }
  346. TEST(BitVectorTest, BeginEnd) {
  347. storm::storage::BitVector vector(32);
  348. ASSERT_TRUE(vector.begin() == vector.end());
  349. vector.set(17);
  350. ASSERT_FALSE(vector.begin() == vector.end());
  351. vector.set(17, false);
  352. ASSERT_TRUE(vector.begin() == vector.end());
  353. }
  354. TEST(BitVectorTest, NextSetIndex) {
  355. storm::storage::BitVector vector(32);
  356. vector.set(14);
  357. vector.set(17);
  358. ASSERT_EQ(14, vector.getNextSetIndex(14));
  359. ASSERT_EQ(17, vector.getNextSetIndex(15));
  360. ASSERT_EQ(17, vector.getNextSetIndex(16));
  361. ASSERT_EQ(17, vector.getNextSetIndex(17));
  362. ASSERT_EQ(vector.size(), vector.getNextSetIndex(18));
  363. }
  364. TEST(BitVectorTest, Iterator) {
  365. storm::storage::BitVector vector(32);
  366. for (uint_fast64_t i = 0; i < 32; ++i) {
  367. vector.set(i, i % 2 == 0);
  368. }
  369. for (auto bit : vector) {
  370. ASSERT_TRUE(bit % 2 == 0);
  371. }
  372. }