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.

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