handle.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2010-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/handlealloc.h>
  7. TEST(HandleListT)
  8. {
  9. bx::HandleListT<32> list;
  10. list.pushBack(16);
  11. CHECK(list.getFront() == 16);
  12. CHECK(list.getBack() == 16);
  13. list.pushFront(7);
  14. CHECK(list.getFront() == 7);
  15. CHECK(list.getBack() == 16);
  16. uint16_t expected0[] = { 15, 31, 7, 16, 17, 11, 13 };
  17. list.pushBack(17);
  18. list.pushBack(11);
  19. list.pushBack(13);
  20. list.pushFront(31);
  21. list.pushFront(15);
  22. uint16_t count = 0;
  23. for (uint16_t it = list.getFront(); it != UINT16_MAX; it = list.getNext(it), ++count)
  24. {
  25. CHECK(it == expected0[count]);
  26. }
  27. CHECK(count == BX_COUNTOF(expected0) );
  28. list.remove(17);
  29. list.remove(31);
  30. list.remove(16);
  31. list.pushBack(16);
  32. uint16_t expected1[] = { 15, 7, 11, 13, 16 };
  33. count = 0;
  34. for (uint16_t it = list.getFront(); it != UINT16_MAX; it = list.getNext(it), ++count)
  35. {
  36. CHECK(it == expected1[count]);
  37. }
  38. CHECK(count == BX_COUNTOF(expected1) );
  39. list.popBack();
  40. list.popFront();
  41. list.popBack();
  42. list.popBack();
  43. CHECK(list.getFront() == 7);
  44. CHECK(list.getBack() == 7);
  45. list.popBack();
  46. CHECK(list.getFront() == UINT16_MAX);
  47. CHECK(list.getBack() == UINT16_MAX);
  48. }
  49. TEST(HandleAllocLruT)
  50. {
  51. bx::HandleAllocLruT<16> lru;
  52. uint16_t handle[4] =
  53. {
  54. lru.alloc(),
  55. lru.alloc(),
  56. lru.alloc(),
  57. lru.alloc(),
  58. };
  59. lru.touch(handle[1]);
  60. uint16_t expected0[] = { handle[1], handle[3], handle[2], handle[0] };
  61. uint16_t count = 0;
  62. for (uint16_t it = lru.getFront(); it != UINT16_MAX; it = lru.getNext(it), ++count)
  63. {
  64. CHECK(it == expected0[count]);
  65. }
  66. }