BufferTests.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "tests/BufferTests.h"
  2. #include "test/Test.h"
  3. #include "utils/Buffer.h"
  4. static constexpr int SIZE_TYPES =
  5. sizeof(int) + sizeof(long) + sizeof(float) + sizeof(double);
  6. static void testAdd() {
  7. Core::Buffer buffer(10);
  8. for(int i = 0; i < 100000; 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 * 100000, 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(Core::memoryCompare(buffer, buffer2, SIZE_TYPES * 10));
  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::BufferTests::test() {
  57. testAdd();
  58. testCopy();
  59. testMoveConstruct();
  60. testMove();
  61. }