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.

507 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. }
  252. TEST(BitVectorTest, OperatorNot) {
  253. storm::storage::BitVector vector1(32);
  254. storm::storage::BitVector vector2(32);
  255. for (uint_fast64_t i = 0; i < 32; ++i) {
  256. vector1.set(i, i % 2 == 0);
  257. vector2.set(i, i % 2 == 1);
  258. }
  259. storm::storage::BitVector notResult = ~vector2;
  260. for (uint_fast64_t i = 0; i < 32; ++i) {
  261. ASSERT_EQ(vector1.get(i), notResult.get(i));
  262. }
  263. }
  264. TEST(BitVectorTest, Complement) {
  265. storm::storage::BitVector vector1(32);
  266. storm::storage::BitVector vector2(32);
  267. for (uint_fast64_t i = 0; i < 32; ++i) {
  268. vector1.set(i, i % 2 == 0);
  269. vector2.set(i, i % 2 == 1);
  270. }
  271. vector2.complement();
  272. for (uint_fast64_t i = 0; i < 32; ++i) {
  273. ASSERT_EQ(vector1.get(i), vector2.get(i));
  274. }
  275. }
  276. TEST(BitVectorTest, Implies) {
  277. storm::storage::BitVector vector1(32);
  278. storm::storage::BitVector vector2(32, true);
  279. for (uint_fast64_t i = 0; i < 32; ++i) {
  280. vector1.set(i, i % 2 == 0);
  281. }
  282. vector2.set(31, false);
  283. vector2.set(30, false);
  284. storm::storage::BitVector impliesResult = vector1.implies(vector2);
  285. for (uint_fast64_t i = 0; i < 30; ++i) {
  286. ASSERT_TRUE(impliesResult.get(i));
  287. }
  288. ASSERT_FALSE(impliesResult.get(30));
  289. ASSERT_TRUE(impliesResult.get(31));
  290. }
  291. TEST(BitVectorTest, Subset) {
  292. storm::storage::BitVector vector1(32);
  293. storm::storage::BitVector vector2(32, true);
  294. for (uint_fast64_t i = 0; i < 32; ++i) {
  295. vector1.set(i, i % 2 == 0);
  296. }
  297. ASSERT_TRUE(vector1.isSubsetOf(vector2));
  298. vector2.set(16, false);
  299. ASSERT_FALSE(vector1.isSubsetOf(vector2));
  300. }
  301. TEST(BitVectorTest, Disjoint) {
  302. storm::storage::BitVector vector1(32);
  303. storm::storage::BitVector vector2(32);
  304. for (uint_fast64_t i = 0; i < 32; ++i) {
  305. vector1.set(i, i % 2 == 0);
  306. vector2.set(i, i % 2 == 1);
  307. }
  308. ASSERT_TRUE(vector1.isDisjointFrom(vector2));
  309. vector2.set(16, true);
  310. ASSERT_FALSE(vector1.isDisjointFrom(vector2));
  311. }
  312. TEST(BitVectorTest, Empty) {
  313. storm::storage::BitVector vector(32);
  314. ASSERT_TRUE(vector.empty());
  315. vector.set(17, true);
  316. ASSERT_FALSE(vector.empty());
  317. vector.set(17, false);
  318. vector.set(18, false);
  319. ASSERT_TRUE(vector.empty());
  320. }
  321. TEST(BitVectorTest, Full) {
  322. storm::storage::BitVector vector(32, true);
  323. ASSERT_TRUE(vector.full());
  324. vector.set(17, false);
  325. ASSERT_FALSE(vector.full());
  326. vector.set(17, true);
  327. vector.set(18, true);
  328. ASSERT_TRUE(vector.full());
  329. }
  330. TEST(BitVectorTest, NumberOfSetBits) {
  331. storm::storage::BitVector vector(32);
  332. for (uint_fast64_t i = 0; i < 32; ++i) {
  333. vector.set(i, i % 2 == 0);
  334. }
  335. ASSERT_EQ(16ul, vector.getNumberOfSetBits());
  336. }
  337. TEST(BitVectorTest, NumberOfSetBitsBeforeIndex) {
  338. storm::storage::BitVector vector(32);
  339. for (uint_fast64_t i = 0; i < 32; ++i) {
  340. vector.set(i, i % 2 == 0);
  341. }
  342. ASSERT_EQ(7ul, vector.getNumberOfSetBitsBeforeIndex(14));
  343. }
  344. TEST(BitVectorTest, BeginEnd) {
  345. storm::storage::BitVector vector(32);
  346. ASSERT_TRUE(vector.begin() == vector.end());
  347. vector.set(17);
  348. ASSERT_FALSE(vector.begin() == vector.end());
  349. vector.set(17, false);
  350. ASSERT_TRUE(vector.begin() == vector.end());
  351. }
  352. TEST(BitVectorTest, NextSetIndex) {
  353. storm::storage::BitVector vector(32);
  354. vector.set(14);
  355. vector.set(17);
  356. ASSERT_EQ(14ul, vector.getNextSetIndex(14));
  357. ASSERT_EQ(17ul, vector.getNextSetIndex(15));
  358. ASSERT_EQ(17ul, vector.getNextSetIndex(16));
  359. ASSERT_EQ(17ul, vector.getNextSetIndex(17));
  360. ASSERT_EQ(vector.size(), vector.getNextSetIndex(18));
  361. }
  362. TEST(BitVectorTest, Iterator) {
  363. storm::storage::BitVector vector(32);
  364. for (uint_fast64_t i = 0; i < 32; ++i) {
  365. vector.set(i, i % 2 == 0);
  366. }
  367. for (auto bit : vector) {
  368. ASSERT_TRUE(bit % 2 == 0);
  369. }
  370. }
  371. TEST(BitVectorTest, CompareAndSwap) {
  372. storm::storage::BitVector vector(140);
  373. vector.setFromInt(0, 64, 2377830234574424100);
  374. vector.setFromInt(64, 64, 1152921504607379586);
  375. vector.setFromInt(128, 12, 2080);
  376. bool result = vector.compareAndSwap(0, 68, 68);
  377. ASSERT_FALSE(result);
  378. result = vector.compareAndSwap(68, 0, 68);
  379. ASSERT_TRUE(result);
  380. }