Browse Source

typed buffer and tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
ce80f29511
5 changed files with 65 additions and 0 deletions
  1. 2 0
      Main.cpp
  2. 1 0
      meson.build
  3. 21 0
      tests/TypedBufferTests.cpp
  4. 8 0
      tests/TypedBufferTests.h
  5. 33 0
      utils/TypedBuffer.h

+ 2 - 0
Main.cpp

@@ -21,6 +21,7 @@
 #include "tests/PNGReaderTests.h"
 #include "tests/BufferTests.h"
 #include "tests/StackAllocatorTests.h"
+#include "tests/TypedBufferTests.h"
 #include "wrapper/Framebuffer.h"
 #include "rendering/FileTexture.h"
 
@@ -50,5 +51,6 @@ int main(int argAmount, char** args) {
     PNGReaderTests::test(args[1]);
     BufferTests::test();
     StackAllocatorTests::test();
+    TypedBufferTests::test();
     return 0;
 }

+ 1 - 0
meson.build

@@ -31,6 +31,7 @@ sources = ['Main.cpp',
     'images/PNGReader.cpp',
     'tests/PNGReaderTests.cpp',
     'tests/BufferTests.cpp',
+    'tests/TypedBufferTests.cpp',
     'wrapper/Texture.cpp',
     'wrapper/GL.cpp',
     'wrapper/GLFW.cpp',

+ 21 - 0
tests/TypedBufferTests.cpp

@@ -0,0 +1,21 @@
+#include "tests/TypedBufferTests.h"
+#include "tests/Test.h"
+#include "utils/TypedBuffer.h"
+
+static void testAdd(Test& test) {
+    TypedBuffer<int> buffer(10);
+    for(int i = 0; i < 100000; i++) {
+        buffer.add(5);
+        buffer.add(5L);
+        buffer.add(5.0f);
+        buffer.add(5.0);
+    }
+    test.checkEqual(400000, buffer.getLength(), "add increments length");
+    test.checkEqual(static_cast<int> ((sizeof (int)) * 400000), buffer.getByteLength(), "add increments byte length");
+}
+
+void TypedBufferTests::test() {
+    Test test("TypedBuffer");
+    testAdd(test);
+    test.finalize();
+}

+ 8 - 0
tests/TypedBufferTests.h

@@ -0,0 +1,8 @@
+#ifndef TYPEDBUFFERTESTS_H
+#define TYPEDBUFFERTESTS_H
+
+namespace TypedBufferTests {
+    void test();
+}
+
+#endif

+ 33 - 0
utils/TypedBuffer.h

@@ -0,0 +1,33 @@
+#ifndef TYPEBUFFER_H
+#define TYPEBUFFER_H
+
+#include "utils/Buffer.h"
+
+template<typename T>
+class TypedBuffer {
+    Buffer data;
+
+public:
+
+    TypedBuffer(int n) : data(sizeof (T) * n) {
+    }
+
+    TypedBuffer& add(const T& t) {
+        data.add(t);
+        return *this;
+    }
+
+    int getLength() const {
+        return data.getLength() / sizeof (T);
+    }
+
+    int getByteLength() const {
+        return data.getLength();
+    }
+
+    operator const T*() const {
+        return reinterpret_cast<const T*>(static_cast<const char*>(data));
+    }
+};
+
+#endif