BufferTests.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "../Tests.hpp"
  2. #include "core/utils/Buffer.hpp"
  3. static constexpr size_t SIZE_TYPES =
  4. sizeof(int) + sizeof(long) + sizeof(float) + sizeof(double);
  5. static void testAdd(bool light) {
  6. Core::Buffer buffer(10);
  7. size_t limit = light ? 1000 : 100000;
  8. for(size_t i = 0; i < limit; i++) {
  9. CORE_TEST_ERROR(buffer.add(5));
  10. CORE_TEST_ERROR(buffer.add(5L));
  11. CORE_TEST_ERROR(buffer.add(5.0f));
  12. CORE_TEST_ERROR(buffer.add(5.0));
  13. }
  14. CORE_TEST_EQUAL(SIZE_TYPES * limit, buffer.getLength());
  15. }
  16. static void testCopy() {
  17. Core::Buffer buffer(10);
  18. for(int i = 0; i < 10; i++) {
  19. CORE_TEST_ERROR(buffer.add(5));
  20. CORE_TEST_ERROR(buffer.add(5L));
  21. CORE_TEST_ERROR(buffer.add(5.0f));
  22. CORE_TEST_ERROR(buffer.add(5.0));
  23. }
  24. Core::Buffer buffer2;
  25. CORE_TEST_ERROR(buffer2.copyFrom(buffer));
  26. if(CORE_TEST_EQUAL(SIZE_TYPES * 10, buffer.getLength()) &&
  27. CORE_TEST_EQUAL(SIZE_TYPES * 10, buffer2.getLength())) {
  28. CORE_TEST_TRUE(memcmp(buffer, buffer2, SIZE_TYPES * 10) == 0);
  29. }
  30. }
  31. static void testMoveConstruct() {
  32. Core::Buffer buffer(10);
  33. for(int i = 0; i < 10; i++) {
  34. CORE_TEST_ERROR(buffer.add(5));
  35. CORE_TEST_ERROR(buffer.add(5L));
  36. CORE_TEST_ERROR(buffer.add(5.0f));
  37. CORE_TEST_ERROR(buffer.add(5.0));
  38. }
  39. Core::Buffer buffer2(Core::move(buffer));
  40. CORE_TEST_EQUAL(0, buffer.getLength());
  41. CORE_TEST_EQUAL(SIZE_TYPES * 10, buffer2.getLength());
  42. }
  43. static void testMove() {
  44. Core::Buffer buffer(10);
  45. for(int i = 0; i < 10; i++) {
  46. CORE_TEST_ERROR(buffer.add(5));
  47. CORE_TEST_ERROR(buffer.add(5L));
  48. CORE_TEST_ERROR(buffer.add(5.0f));
  49. CORE_TEST_ERROR(buffer.add(5.0));
  50. }
  51. Core::Buffer buffer2(3);
  52. buffer2 = Core::move(buffer);
  53. CORE_TEST_EQUAL(0, buffer.getLength());
  54. CORE_TEST_EQUAL(SIZE_TYPES * 10, buffer2.getLength());
  55. }
  56. void Core::testBuffer(bool light) {
  57. testAdd(light);
  58. testCopy();
  59. testMoveConstruct();
  60. testMove();
  61. }