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.

468 lines
11 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.setFromInt(62, 4, 15);
  77. EXPECT_TRUE(vector.get(62));
  78. EXPECT_TRUE(vector.get(63));
  79. EXPECT_TRUE(vector.get(64));
  80. EXPECT_TRUE(vector.get(65));
  81. vector.setFromInt(62, 5, 17);
  82. }
  83. TEST(BitVectorDeathTest, GetSetAssertion) {
  84. storm::storage::BitVector vector(32);
  85. EXPECT_DEATH_IF_SUPPORTED(vector.get(32), "");
  86. EXPECT_DEATH_IF_SUPPORTED(vector.set(32), "");
  87. }
  88. TEST(BitVectorTest, Resize) {
  89. storm::storage::BitVector vector(32);
  90. for (uint_fast64_t i = 0; i < 32; ++i) {
  91. vector.set(i);
  92. }
  93. vector.resize(70);
  94. ASSERT_EQ(70, vector.size());
  95. ASSERT_EQ(32, vector.getNumberOfSetBits());
  96. for (uint_fast64_t i = 0; i < 32; ++i) {
  97. ASSERT_TRUE(vector.get(i));
  98. }
  99. bool result;
  100. for (uint_fast64_t i = 32; i < 70; ++i) {
  101. result = true;
  102. ASSERT_NO_THROW(result = vector.get(i));
  103. ASSERT_FALSE(result);
  104. }
  105. vector.resize(72, true);
  106. ASSERT_EQ(72, vector.size());
  107. ASSERT_EQ(34, vector.getNumberOfSetBits());
  108. for (uint_fast64_t i = 0; i < 32; ++i) {
  109. ASSERT_TRUE(vector.get(i));
  110. }
  111. for (uint_fast64_t i = 32; i < 70; ++i) {
  112. result = true;
  113. ASSERT_NO_THROW(result = vector.get(i));
  114. ASSERT_FALSE(result);
  115. }
  116. for (uint_fast64_t i = 70; i < 72; ++i) {
  117. ASSERT_TRUE(vector.get(i));
  118. }
  119. vector.resize(16, 0);
  120. ASSERT_EQ(16, vector.size());
  121. ASSERT_EQ(16, vector.getNumberOfSetBits());
  122. for (uint_fast64_t i = 0; i < 16; ++i) {
  123. ASSERT_TRUE(vector.get(i));
  124. }
  125. vector.resize(65, 1);
  126. ASSERT_EQ(65, vector.size());
  127. ASSERT_TRUE(vector.full());
  128. }
  129. TEST(BitVectorTest, OperatorAnd) {
  130. storm::storage::BitVector vector1(32);
  131. storm::storage::BitVector vector2(32);
  132. for (int i = 0; i < 32; ++i) {
  133. vector1.set(i, i % 2 == 0);
  134. vector2.set(i, i % 2 == 1);
  135. }
  136. vector1.set(31);
  137. vector2.set(31);
  138. storm::storage::BitVector andResult = vector1 & vector2;
  139. for (uint_fast64_t i = 0; i < 31; ++i) {
  140. ASSERT_FALSE(andResult.get(i));
  141. }
  142. ASSERT_TRUE(andResult.get(31));
  143. }
  144. TEST(BitVectorTest, OperatorAndEqual) {
  145. storm::storage::BitVector vector1(32);
  146. storm::storage::BitVector vector2(32);
  147. for (int i = 0; i < 32; ++i) {
  148. vector1.set(i, i % 2 == 0);
  149. vector2.set(i, i % 2 == 1);
  150. }
  151. vector1.set(31);
  152. vector2.set(31);
  153. vector1 &= vector2;
  154. for (uint_fast64_t i = 0; i < 31; ++i) {
  155. ASSERT_FALSE(vector1.get(i));
  156. }
  157. ASSERT_TRUE(vector1.get(31));
  158. }
  159. TEST(BitVectorTest, OperatorOr) {
  160. storm::storage::BitVector vector1(32);
  161. storm::storage::BitVector vector2(32);
  162. for (uint_fast64_t i = 0; i < 32; ++i) {
  163. vector1.set(i, i % 2 == 0);
  164. vector2.set(i, i % 2 == 1);
  165. }
  166. vector1.set(31, false);
  167. vector2.set(31, false);
  168. storm::storage::BitVector orResult = vector1 | vector2;
  169. for (uint_fast64_t i = 0; i < 31; ++i) {
  170. ASSERT_TRUE(orResult.get(i));
  171. }
  172. ASSERT_FALSE(orResult.get(31));
  173. }
  174. TEST(BitVectorTest, OperatorOrEqual) {
  175. storm::storage::BitVector vector1(32);
  176. storm::storage::BitVector vector2(32);
  177. for (uint_fast64_t i = 0; i < 32; ++i) {
  178. vector1.set(i, i % 2 == 0);
  179. vector2.set(i, i % 2 == 1);
  180. }
  181. vector1.set(31, false);
  182. vector2.set(31, false);
  183. vector1 |= vector2;
  184. for (uint_fast64_t i = 0; i < 31; ++i) {
  185. ASSERT_TRUE(vector1.get(i));
  186. }
  187. ASSERT_FALSE(vector1.get(31));
  188. }
  189. TEST(BitVectorTest, OperatorXor) {
  190. storm::storage::BitVector vector1(32);
  191. storm::storage::BitVector vector2(32);
  192. for (uint_fast64_t i = 0; i < 32; ++i) {
  193. vector1.set(i);
  194. vector2.set(i, i % 2 == 1);
  195. }
  196. storm::storage::BitVector vector3 = vector1 ^ vector2;
  197. storm::storage::BitVector vector4 = ~vector2;
  198. storm::storage::BitVector vector5 = vector1 ^ vector1;
  199. for (uint_fast64_t i = 0; i < 32; ++i) {
  200. ASSERT_EQ(vector3.get(i), vector4.get(i));
  201. ASSERT_FALSE(vector5.get(i));
  202. }
  203. }
  204. TEST(BitVectorTest, OperatorModulo) {
  205. storm::storage::BitVector vector1(32);
  206. storm::storage::BitVector vector2(32);
  207. for (uint_fast64_t i = 0; i < 15; ++i) {
  208. vector2.set(i, i % 2 == 0);
  209. }
  210. vector1.set(2);
  211. vector1.set(5);
  212. vector1.set(6);
  213. storm::storage::BitVector moduloResult = vector1 % vector2;
  214. ASSERT_EQ(moduloResult.size(), 8);
  215. ASSERT_EQ(moduloResult.getNumberOfSetBits(), 2);
  216. for (uint_fast64_t i = 0; i < 8; ++i) {
  217. if (i == 1 || i == 3) {
  218. ASSERT_TRUE(moduloResult.get(i));
  219. } else {
  220. ASSERT_FALSE(moduloResult.get(i));
  221. }
  222. }
  223. storm::storage::BitVector vector3(31);
  224. for (uint_fast64_t i = 0; i < 15; ++i) {
  225. vector3.set(i, i % 2 == 0);
  226. }
  227. EXPECT_DEATH_IF_SUPPORTED(vector1 % vector3, "");
  228. }
  229. TEST(BitVectorTest, OperatorNot) {
  230. storm::storage::BitVector vector1(32);
  231. storm::storage::BitVector vector2(32);
  232. for (uint_fast64_t i = 0; i < 32; ++i) {
  233. vector1.set(i, i % 2 == 0);
  234. vector2.set(i, i % 2 == 1);
  235. }
  236. storm::storage::BitVector notResult = ~vector2;
  237. for (uint_fast64_t i = 0; i < 32; ++i) {
  238. ASSERT_EQ(vector1.get(i), notResult.get(i));
  239. }
  240. }
  241. TEST(BitVectorTest, Complement) {
  242. storm::storage::BitVector vector1(32);
  243. storm::storage::BitVector vector2(32);
  244. for (uint_fast64_t i = 0; i < 32; ++i) {
  245. vector1.set(i, i % 2 == 0);
  246. vector2.set(i, i % 2 == 1);
  247. }
  248. vector2.complement();
  249. for (uint_fast64_t i = 0; i < 32; ++i) {
  250. ASSERT_EQ(vector1.get(i), vector2.get(i));
  251. }
  252. }
  253. TEST(BitVectorTest, Implies) {
  254. storm::storage::BitVector vector1(32);
  255. storm::storage::BitVector vector2(32, true);
  256. for (uint_fast64_t i = 0; i < 32; ++i) {
  257. vector1.set(i, i % 2 == 0);
  258. }
  259. vector2.set(31, false);
  260. vector2.set(30, false);
  261. storm::storage::BitVector impliesResult = vector1.implies(vector2);
  262. for (uint_fast64_t i = 0; i < 30; ++i) {
  263. ASSERT_TRUE(impliesResult.get(i));
  264. }
  265. ASSERT_FALSE(impliesResult.get(30));
  266. ASSERT_TRUE(impliesResult.get(31));
  267. }
  268. TEST(BitVectorTest, Subset) {
  269. storm::storage::BitVector vector1(32);
  270. storm::storage::BitVector vector2(32, true);
  271. for (uint_fast64_t i = 0; i < 32; ++i) {
  272. vector1.set(i, i % 2 == 0);
  273. }
  274. ASSERT_TRUE(vector1.isSubsetOf(vector2));
  275. vector2.set(16, false);
  276. ASSERT_FALSE(vector1.isSubsetOf(vector2));
  277. }
  278. TEST(BitVectorTest, Disjoint) {
  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. ASSERT_TRUE(vector1.isDisjointFrom(vector2));
  286. vector2.set(16, true);
  287. ASSERT_FALSE(vector1.isDisjointFrom(vector2));
  288. }
  289. TEST(BitVectorTest, Empty) {
  290. storm::storage::BitVector vector(32);
  291. ASSERT_TRUE(vector.empty());
  292. vector.set(17, true);
  293. ASSERT_FALSE(vector.empty());
  294. vector.set(17, false);
  295. vector.set(18, false);
  296. ASSERT_TRUE(vector.empty());
  297. }
  298. TEST(BitVectorTest, Full) {
  299. storm::storage::BitVector vector(32, true);
  300. ASSERT_TRUE(vector.full());
  301. vector.set(17, false);
  302. ASSERT_FALSE(vector.full());
  303. vector.set(17, true);
  304. vector.set(18, true);
  305. ASSERT_TRUE(vector.full());
  306. }
  307. TEST(BitVectorTest, NumberOfSetBits) {
  308. storm::storage::BitVector vector(32);
  309. for (uint_fast64_t i = 0; i < 32; ++i) {
  310. vector.set(i, i % 2 == 0);
  311. }
  312. ASSERT_EQ(16, vector.getNumberOfSetBits());
  313. }
  314. TEST(BitVectorTest, NumberOfSetBitsBeforeIndex) {
  315. storm::storage::BitVector vector(32);
  316. for (uint_fast64_t i = 0; i < 32; ++i) {
  317. vector.set(i, i % 2 == 0);
  318. }
  319. ASSERT_EQ(7, vector.getNumberOfSetBitsBeforeIndex(14));
  320. }
  321. TEST(BitVectorTest, BeginEnd) {
  322. storm::storage::BitVector vector(32);
  323. ASSERT_TRUE(vector.begin() == vector.end());
  324. vector.set(17);
  325. ASSERT_FALSE(vector.begin() == vector.end());
  326. vector.set(17, false);
  327. ASSERT_TRUE(vector.begin() == vector.end());
  328. }
  329. TEST(BitVectorTest, NextSetIndex) {
  330. storm::storage::BitVector vector(32);
  331. vector.set(14);
  332. vector.set(17);
  333. ASSERT_EQ(14, vector.getNextSetIndex(14));
  334. ASSERT_EQ(17, vector.getNextSetIndex(15));
  335. ASSERT_EQ(17, vector.getNextSetIndex(16));
  336. ASSERT_EQ(17, vector.getNextSetIndex(17));
  337. ASSERT_EQ(vector.size(), vector.getNextSetIndex(18));
  338. }
  339. TEST(BitVectorTest, Iterator) {
  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. for (auto bit : vector) {
  345. ASSERT_TRUE(bit % 2 == 0);
  346. }
  347. }