BufferTests.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "tests/BufferTests.h"
  2. #include "tests/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(Test& test) {
  7. Buffer buffer(10);
  8. for(int i = 0; i < 100000; i++) {
  9. buffer.add(5);
  10. buffer.add(5L);
  11. buffer.add(5.0f);
  12. buffer.add(5.0);
  13. }
  14. test.checkEqual(SIZE_TYPES * 100000, buffer.getLength(),
  15. "add increments length");
  16. }
  17. static void testCopy(Test& test) {
  18. Buffer buffer(10);
  19. for(int i = 0; i < 10; i++) {
  20. buffer.add(5);
  21. buffer.add(5L);
  22. buffer.add(5.0f);
  23. buffer.add(5.0);
  24. }
  25. Buffer buffer2(buffer);
  26. Buffer buffer3(2);
  27. buffer3 = buffer;
  28. test.checkEqual(SIZE_TYPES * 10, buffer.getLength(), "copy");
  29. test.checkEqual(SIZE_TYPES * 10, buffer2.getLength(), "copy");
  30. test.checkEqual(SIZE_TYPES * 10, buffer3.getLength(), "copy");
  31. }
  32. static void testMoveConstruct(Test& test) {
  33. Buffer buffer(10);
  34. for(int i = 0; i < 10; i++) {
  35. buffer.add(5);
  36. buffer.add(5L);
  37. buffer.add(5.0f);
  38. buffer.add(5.0);
  39. }
  40. Buffer buffer2(std::move(buffer));
  41. test.checkEqual(0, buffer.getLength(), "move");
  42. test.checkEqual(SIZE_TYPES * 10, buffer2.getLength(), "move");
  43. }
  44. static void testMove(Test& test) {
  45. Buffer buffer(10);
  46. for(int i = 0; i < 10; i++) {
  47. buffer.add(5);
  48. buffer.add(5L);
  49. buffer.add(5.0f);
  50. buffer.add(5.0);
  51. }
  52. Buffer buffer2(3);
  53. buffer2 = std::move(buffer);
  54. test.checkEqual(0, buffer.getLength(), "move");
  55. test.checkEqual(SIZE_TYPES * 10, buffer2.getLength(), "move");
  56. }
  57. void BufferTests::test() {
  58. Test test("Buffer");
  59. testAdd(test);
  60. testCopy(test);
  61. testMoveConstruct(test);
  62. testMove(test);
  63. test.finalize();
  64. }