Browse Source

buffer and tests, attribute spacers without argument and correctly
calculated offset

Kajetan Johannes Hammerle 3 years ago
parent
commit
b8f6e4c345
7 changed files with 70 additions and 3 deletions
  1. 2 0
      Main.cpp
  2. 1 0
      meson.build
  3. 28 0
      tests/BufferTests.cpp
  4. 8 0
      tests/BufferTests.h
  5. 28 0
      utils/Buffer.h
  6. 2 2
      wrapper/Attributes.cpp
  7. 1 1
      wrapper/Attributes.h

+ 2 - 0
Main.cpp

@@ -19,6 +19,7 @@
 #include "tests/ColorTests.h"
 #include "tests/ClockTests.h"
 #include "tests/PNGReaderTests.h"
+#include "tests/BufferTests.h"
 
 int main(int argAmount, char** args) {
     if(argAmount < 2) {
@@ -44,5 +45,6 @@ int main(int argAmount, char** args) {
     ColorTests::test();
     ClockTests::test();
     PNGReaderTests::test(args[1]);
+    BufferTests::test();
     return 0;
 }

+ 1 - 0
meson.build

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

+ 28 - 0
tests/BufferTests.cpp

@@ -0,0 +1,28 @@
+#include "tests/BufferTests.h"
+#include "tests/Test.h"
+#include "utils/Buffer.h"
+
+static void testAdd(Test& test) {
+    Buffer<10> bits;
+    int a = 5;
+    bits.add(a);
+    test.checkEqual(static_cast<int> (sizeof (a)), bits.getLength(), "add increments length");
+}
+
+static void testOverflow(Test& test) {
+    Buffer<10> bits;
+    for(int i = 0; i < 1000000; i++) {
+        bits.add(5);
+        bits.add(5L);
+        bits.add(5.0f);
+        bits.add(5.0);
+    }
+    test.checkEqual(10, bits.getLength(), "add increments length");
+}
+
+void BufferTests::test() {
+    Test test("Buffer");
+    testAdd(test);
+    testOverflow(test);
+    test.finalize();
+}

+ 8 - 0
tests/BufferTests.h

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

+ 28 - 0
utils/Buffer.h

@@ -0,0 +1,28 @@
+#ifndef BUFFER_H
+#define BUFFER_H
+
+#include <cstring>
+
+template<int N>
+class Buffer {
+    char data[N];
+    int length = 0;
+
+public:
+    template<typename T>
+    Buffer& add(const T& t) {
+        int bytes = N - length;
+        if(bytes > static_cast<int> (sizeof (T))) {
+            bytes = sizeof (T);
+        }
+        memcpy(data + length, &t, bytes);
+        length += bytes;
+        return *this;
+    }
+
+    int getLength() const {
+        return length;
+    }
+};
+
+#endif

+ 2 - 2
wrapper/Attributes.cpp

@@ -12,8 +12,8 @@ Attributes& Attributes::addColor4() {
     return *this;
 }
 
-Attributes& Attributes::addSpacer(int count, int size) {
-    attributes.add({count, size, -1, false});
+Attributes& Attributes::addSpacer() {
+    attributes.add({0, 0, -1, false});
     return *this;
 }
 

+ 1 - 1
wrapper/Attributes.h

@@ -19,7 +19,7 @@ class Attributes final {
 public:
     Attributes& addFloat(int count);
     Attributes& addColor4();
-    Attributes& addSpacer(int count, int size);
+    Attributes& addSpacer();
 
 private:
     void set() const;