test_bitmask.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <AP_gtest.h>
  2. #include <AP_Common/Bitmask.h>
  3. TEST(Bitmask, Tests)
  4. {
  5. Bitmask<49> x;
  6. EXPECT_EQ(-1, x.first_set());
  7. x.set(5);
  8. EXPECT_EQ(5, x.first_set());
  9. x.clear(5);
  10. EXPECT_EQ(-1, x.first_set());
  11. EXPECT_EQ(-1, x.first_set());
  12. x.set(42);
  13. EXPECT_EQ(42, x.first_set());
  14. x.clear(42);
  15. EXPECT_EQ(-1, x.first_set());
  16. EXPECT_EQ(-1, x.first_set());
  17. x.set(0);
  18. x.set(5);
  19. x.set(6);
  20. x.set(48);
  21. EXPECT_EQ(0, x.first_set());
  22. EXPECT_EQ(0, x.first_set());
  23. x.clear(0);
  24. EXPECT_EQ(5, x.first_set());
  25. EXPECT_EQ(5, x.first_set());
  26. x.clear(5);
  27. EXPECT_EQ(6, x.first_set());
  28. EXPECT_EQ(6, x.first_set());
  29. x.clear(6);
  30. EXPECT_EQ(48, x.first_set());
  31. EXPECT_EQ(48, x.first_set());
  32. x.clear(48);
  33. EXPECT_EQ(-1, x.first_set());
  34. }
  35. TEST(Bitmask, SetAll)
  36. {
  37. Bitmask<49> x;
  38. EXPECT_EQ(-1, x.first_set());
  39. EXPECT_EQ(false, x.get(45));
  40. x.setall();
  41. EXPECT_EQ(0, x.first_set());
  42. x.clear(0);
  43. EXPECT_EQ(1, x.first_set());
  44. x.clear(1);
  45. EXPECT_EQ(2, x.first_set());
  46. EXPECT_EQ(true, x.get(45));
  47. EXPECT_EQ(false, x.empty());
  48. x.clearall();
  49. EXPECT_EQ(-1, x.first_set());
  50. EXPECT_EQ(false, x.get(45));
  51. EXPECT_EQ(true, x.empty());
  52. }
  53. TEST(Bitmask, Assignment)
  54. {
  55. Bitmask<49> x;
  56. x.set(0);
  57. x.set(5);
  58. x.set(6);
  59. x.set(48);
  60. Bitmask<49> y;
  61. y = x;
  62. x.clear(0);
  63. EXPECT_EQ(true, y.get(0));
  64. EXPECT_EQ(true, y.get(5));
  65. EXPECT_EQ(true, y.get(6));
  66. EXPECT_EQ(true, y.get(48));
  67. }
  68. AP_GTEST_MAIN()
  69. int hal = 0; // bizarrely, this fixes an undefined-symbol error but doesn't raise a type exception. Yay.