linked_list.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/util/linked_list.hpp>
  6. struct ListItem : uavcan::LinkedListNode<ListItem>
  7. {
  8. int value;
  9. ListItem(int value = 0)
  10. : value(value)
  11. { }
  12. struct GreaterThanComparator
  13. {
  14. const int compare_with;
  15. GreaterThanComparator(int compare_with)
  16. : compare_with(compare_with)
  17. { }
  18. bool operator()(const ListItem* item) const
  19. {
  20. return item->value > compare_with;
  21. }
  22. };
  23. void insort(uavcan::LinkedListRoot<ListItem>& root)
  24. {
  25. root.insertBefore(this, GreaterThanComparator(value));
  26. }
  27. };
  28. TEST(LinkedList, Basic)
  29. {
  30. uavcan::LinkedListRoot<ListItem> root;
  31. /*
  32. * Insert/remove
  33. */
  34. EXPECT_EQ(0, root.getLength());
  35. ListItem item1;
  36. root.insert(&item1);
  37. root.insert(&item1); // Insert twice - second will be ignored
  38. EXPECT_EQ(1, root.getLength());
  39. root.remove(&item1);
  40. root.remove(&item1);
  41. EXPECT_EQ(0, root.getLength());
  42. ListItem items[3];
  43. root.insert(&item1);
  44. root.insert(items + 0);
  45. root.insert(items + 1);
  46. root.insert(items + 2);
  47. EXPECT_EQ(4, root.getLength());
  48. /*
  49. * Order persistence
  50. */
  51. items[0].value = 10;
  52. items[1].value = 11;
  53. items[2].value = 12;
  54. const int expected_values[] = {12, 11, 10, 0};
  55. ListItem* node = root.get();
  56. for (int i = 0; i < 4; i++)
  57. {
  58. EXPECT_EQ(expected_values[i], node->value);
  59. node = node->getNextListNode();
  60. }
  61. root.remove(items + 0);
  62. root.remove(items + 2);
  63. root.remove(items + 2);
  64. EXPECT_EQ(2, root.getLength());
  65. const int expected_values2[] = {11, 0};
  66. node = root.get();
  67. for (int i = 0; i < 2; i++)
  68. {
  69. EXPECT_EQ(expected_values2[i], node->value);
  70. node = node->getNextListNode();
  71. }
  72. root.insert(items + 2);
  73. EXPECT_EQ(3, root.getLength());
  74. EXPECT_EQ(12, root.get()->value);
  75. /*
  76. * Emptying
  77. */
  78. root.remove(&item1);
  79. root.remove(items + 0);
  80. root.remove(items + 1);
  81. root.remove(items + 2);
  82. EXPECT_EQ(0, root.getLength());
  83. }
  84. TEST(LinkedList, Sorting)
  85. {
  86. uavcan::LinkedListRoot<ListItem> root;
  87. ListItem items[6];
  88. for (int i = 0; i < 6; i++)
  89. {
  90. items[i].value = i;
  91. }
  92. EXPECT_EQ(0, root.getLength());
  93. items[2].insort(root);
  94. EXPECT_EQ(1, root.getLength());
  95. items[2].insort(root); // Making sure the duplicate will be removed
  96. EXPECT_EQ(1, root.getLength());
  97. items[3].insort(root);
  98. items[0].insort(root);
  99. items[4].insort(root);
  100. EXPECT_EQ(4, root.getLength());
  101. items[1].insort(root);
  102. EXPECT_EQ(5, root.getLength());
  103. items[1].insort(root); // Another duplicate
  104. EXPECT_EQ(5, root.getLength());
  105. items[5].insort(root);
  106. EXPECT_EQ(6, root.getLength());
  107. int prev_val = -100500;
  108. const ListItem* item = root.get();
  109. while (item)
  110. {
  111. //std::cout << item->value << std::endl;
  112. EXPECT_LT(prev_val, item->value);
  113. prev_val = item->value;
  114. item = item->getNextListNode();
  115. }
  116. }