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.

114 lines
2.2 KiB

  1. #include "gtest/gtest.h"
  2. #include "src/storage/BitVector.h"
  3. #include "src/exceptions/invalid_argument.h"
  4. TEST(BitVectorTest, GetSetTest) {
  5. mrmc::storage::BitVector *bv = NULL;
  6. ASSERT_NO_THROW(bv = new mrmc::storage::BitVector(32));
  7. for (int i = 0; i < 32; ++i) {
  8. bv->set(i, i % 2 == 0);
  9. }
  10. for (int i = 0; i < 32; ++i) {
  11. ASSERT_EQ(bv->get(i), i % 2 == 0);
  12. }
  13. delete bv;
  14. }
  15. TEST(BitVectorTest, InitialZeroTest) {
  16. mrmc::storage::BitVector bvA(32);
  17. for (int i = 0; i < 32; ++i) {
  18. ASSERT_FALSE(bvA.get(i));
  19. }
  20. }
  21. TEST(BitVectorTest, ResizeTest) {
  22. mrmc::storage::BitVector bvA(32);
  23. for (int i = 0; i < 32; ++i) {
  24. bvA.set(i, true);
  25. }
  26. bvA.resize(70);
  27. for (int i = 0; i < 32; ++i) {
  28. ASSERT_TRUE(bvA.get(i));
  29. }
  30. bool result;
  31. for (int i = 32; i < 70; ++i) {
  32. result = true;
  33. ASSERT_NO_THROW(result = bvA.get(i));
  34. ASSERT_FALSE(result);
  35. }
  36. }
  37. TEST(BitVectorTest, OperatorNotTest) {
  38. mrmc::storage::BitVector bvA(32);
  39. mrmc::storage::BitVector bvB(32);
  40. for (int i = 0; i < 32; ++i) {
  41. bvA.set(i, i % 2 == 0);
  42. bvB.set(i, i % 2 == 1);
  43. }
  44. mrmc::storage::BitVector bvN = ~bvB;
  45. for (int i = 0; i < 32; ++i) {
  46. ASSERT_EQ(bvA.get(i), bvN.get(i));
  47. }
  48. }
  49. TEST(BitVectorTest, OperatorAndTest) {
  50. mrmc::storage::BitVector bvA(32);
  51. mrmc::storage::BitVector bvB(32);
  52. for (int i = 0; i < 32; ++i) {
  53. bvA.set(i, i % 2 == 0);
  54. bvB.set(i, i % 2 == 1);
  55. }
  56. mrmc::storage::BitVector bvN = bvA & bvB;
  57. for (int i = 0; i < 32; ++i) {
  58. ASSERT_FALSE(bvN.get(i));
  59. }
  60. }
  61. TEST(BitVectorTest, OperatorOrTest) {
  62. mrmc::storage::BitVector bvA(32);
  63. mrmc::storage::BitVector bvB(32);
  64. for (int i = 0; i < 32; ++i) {
  65. bvA.set(i, i % 2 == 0);
  66. bvB.set(i, i % 2 == 1);
  67. }
  68. mrmc::storage::BitVector bvN = bvA | bvB;
  69. for (int i = 0; i < 32; ++i) {
  70. ASSERT_TRUE(bvN.get(i));
  71. }
  72. }
  73. TEST(BitVectorTest, OperatorXorTest) {
  74. mrmc::storage::BitVector bvA(32);
  75. mrmc::storage::BitVector bvB(32);
  76. for (int i = 0; i < 32; ++i) {
  77. bvA.set(i, true);
  78. bvB.set(i, i % 2 == 1);
  79. }
  80. mrmc::storage::BitVector bvN = bvA ^ bvB;
  81. mrmc::storage::BitVector bvO = ~bvB;
  82. mrmc::storage::BitVector bvP = bvA ^ bvA;
  83. for (int i = 0; i < 32; ++i) {
  84. ASSERT_EQ(bvN.get(i), bvO.get(i));
  85. // A XOR A = 0
  86. ASSERT_FALSE(bvP.get(i));
  87. }
  88. }