Просмотр исходного кода

Buffer and tests, fix alloc in test

Kajetan Johannes Hammerle 1 месяц назад
Родитель
Сommit
1b46e19608
8 измененных файлов с 149 добавлено и 83 удалено
  1. 47 47
      CMakeLists.txt
  2. 19 0
      include/core/utils/Buffer.h
  3. 29 0
      src/Buffer.c
  4. 27 32
      test/Main.c
  5. 2 1
      test/Test.c
  6. 2 0
      test/Test.h
  7. 0 3
      test/Tests.h
  8. 23 0
      test/modules/BufferTests.c

+ 47 - 47
CMakeLists.txt

@@ -6,7 +6,7 @@ set(CMAKE_C_STANDARD 23)
 set(SRC
     "src/Logger.c"
     "src/Utility.c"
-    #"src/Buffer.cpp"
+    "src/Buffer.c"
     #"src/Clock.cpp"
     #"src/Random.cpp"
     #"src/BitArray.cpp"
@@ -27,34 +27,34 @@ set(SRC
 set(SRC_TESTS
     "test/Main.c"
     "test/Test.c"
-    #"test/modules/ArrayTests.cpp"
-    #"test/modules/ArrayStringTests.cpp"
+    "test/modules/BufferTests.c"
     "test/modules/UtilityTests.c"
     #"test/modules/ArrayListTests.cpp"
+    #"test/modules/ArrayStringTests.cpp"
+    #"test/modules/ArrayTests.cpp"
     #"test/modules/BitArrayTests.cpp"
-    #"test/modules/MathTests.cpp"
-    #"test/modules/ListTests.cpp"
-    #"test/modules/LinkedListTests.cpp"
-    #"test/modules/HashMapTests.cpp"
-    #"test/modules/StackTests.cpp"
-    #"test/modules/RingBufferTests.cpp"
-    #"test/modules/ComponentsTests.cpp"
-    #"test/modules/VectorTests.cpp"
-    #"test/modules/QuaternionTests.cpp"
-    #"test/modules/MatrixTests.cpp"
     #"test/modules/BoxTests.cpp"
     #"test/modules/BufferedValueTests.cpp"
-    #"test/modules/PlaneTests.cpp"
+    #"test/modules/ClockTests.cpp"
+    #"test/modules/ColorTests.cpp"
+    #"test/modules/ComponentsTests.cpp"
+    #"test/modules/FileReaderTests.cpp"
     #"test/modules/FrustumTests.cpp"
-    #"test/modules/ViewTests.cpp"
+    #"test/modules/HashMapTests.cpp"
+    #"test/modules/HashedStringTests.cpp"
+    #"test/modules/LinkedListTests.cpp"
+    #"test/modules/ListTests.cpp"
+    #"test/modules/MathTests.cpp"
     #"test/modules/MatrixStackTests.cpp"
-    #"test/modules/ColorTests.cpp"
-    #"test/modules/BufferTests.cpp"
-    #"test/modules/ClockTests.cpp"
+    #"test/modules/MatrixTests.cpp"
+    #"test/modules/PlaneTests.cpp"
+    #"test/modules/QuaternionTests.cpp"
     #"test/modules/RandomTests.cpp"
+    #"test/modules/RingBufferTests.cpp"
+    #"test/modules/StackTests.cpp"
     #"test/modules/ThreadTests.cpp"
-    #"test/modules/FileReaderTests.cpp"
-    #"test/modules/HashedStringTests.cpp"
+    #"test/modules/VectorTests.cpp"
+    #"test/modules/ViewTests.cpp"
 )
 
 set(SRC_PERFORMANCE
@@ -168,40 +168,40 @@ target_sources(core PUBLIC
     FILE_SET HEADERS
     BASE_DIRS include
     FILES 
-#        ./include/core/data/Stack.hpp
-#        ./include/core/data/HashMap.hpp
-#        ./include/core/data/Components.hpp
-#        ./include/core/data/ArrayList.hpp
-#        ./include/core/data/ProbingHashMap.hpp
-#        ./include/core/data/List.hpp
-#        ./include/core/data/LinkedList.hpp
-#        ./include/core/data/BitArray.hpp
-#        ./include/core/data/Array.hpp
-#        ./include/core/data/RingBuffer.hpp
-#        ./include/core/thread/Thread.hpp
-#        ./include/core/utils/HashCode.hpp
+        ./include/core/utils/Buffer.h
         ./include/core/utils/Check.h
-#        ./include/core/utils/Buffer.hpp
-#        ./include/core/utils/Random.hpp
-        ./include/core/utils/Types.h
-#        ./include/core/utils/Color.hpp
+        ./include/core/utils/Error.h
         ./include/core/utils/Logger.h
-#        ./include/core/utils/ArrayString.hpp
+        ./include/core/utils/Types.h
         ./include/core/utils/Utility.h
-#        ./include/core/utils/Clock.hpp
-        ./include/core/utils/Error.h
-#        ./include/core/math/Quaternion.hpp
+#        ./include/core/data/Array.hpp
+#        ./include/core/data/ArrayList.hpp
+#        ./include/core/data/BitArray.hpp
+#        ./include/core/data/Components.hpp
+#        ./include/core/data/HashMap.hpp
+#        ./include/core/data/LinkedList.hpp
+#        ./include/core/data/List.hpp
+#        ./include/core/data/ProbingHashMap.hpp
+#        ./include/core/data/RingBuffer.hpp
+#        ./include/core/data/Stack.hpp
+#        ./include/core/io/File.hpp
+#        ./include/core/io/FileReader.hpp
 #        ./include/core/math/Box.hpp
+#        ./include/core/math/BufferedValue.hpp
 #        ./include/core/math/Frustum.hpp
-#        ./include/core/math/Vector.hpp
+#        ./include/core/math/Math.hpp
 #        ./include/core/math/Matrix.hpp
-#        ./include/core/math/View.hpp
-#        ./include/core/math/BufferedValue.hpp
-#        ./include/core/math/Plane.hpp
 #        ./include/core/math/MatrixStack.hpp
-#        ./include/core/math/Math.hpp
-#        ./include/core/io/File.hpp
-#        ./include/core/io/FileReader.hpp
+#        ./include/core/math/Plane.hpp
+#        ./include/core/math/Quaternion.hpp
+#        ./include/core/math/Vector.hpp
+#        ./include/core/math/View.hpp
+#        ./include/core/thread/Thread.hpp
+#        ./include/core/utils/ArrayString.hpp
+#        ./include/core/utils/Clock.hpp
+#        ./include/core/utils/Color.hpp
+#        ./include/core/utils/HashCode.hpp
+#        ./include/core/utils/Random.hpp
 )
 install(TARGETS core FILE_SET HEADERS)
 

+ 19 - 0
include/core/utils/Buffer.h

@@ -0,0 +1,19 @@
+#ifndef CORE_BUFFER_H
+#define CORE_BUFFER_H
+
+#include <stddef.h>
+
+typedef struct {
+    size_t size;
+    size_t capacity;
+    char* buffer;
+} CoreBuffer;
+
+void coreInitBuffer(CoreBuffer* b);
+void coreDestroyBuffer(CoreBuffer* b);
+void coreAddSizedBufferData(CoreBuffer* b, const void* data, size_t size);
+#define coreAddBufferData(buffer, data)                                        \
+    coreAddSizedBufferData(buffer, data, sizeof(*data))
+void coreClearBuffer(CoreBuffer* b);
+
+#endif

+ 29 - 0
src/Buffer.c

@@ -0,0 +1,29 @@
+#include "core/utils/Buffer.h"
+
+#include <string.h>
+
+#include "core/utils/Utility.h"
+
+void coreInitBuffer(CoreBuffer* b) {
+    b->buffer = nullptr;
+    b->capacity = 0;
+    b->size = 0;
+}
+
+void coreDestroyBuffer(CoreBuffer* b) {
+    coreFree(b->buffer);
+    coreInitBuffer(b);
+}
+
+void coreAddSizedBufferData(CoreBuffer* b, const void* data, size_t size) {
+    while(b->size + size >= b->capacity) {
+        b->capacity = b->capacity == 0 ? 8 : (b->capacity * 5) / 4;
+        b->buffer = coreReallocate(b->buffer, b->capacity);
+    }
+    memcpy(b->buffer + b->size, data, size);
+    b->size += size;
+}
+
+void coreClearBuffer(CoreBuffer* b) {
+    b->size = 0;
+}

+ 27 - 32
test/Main.c

@@ -30,39 +30,34 @@ int main(int argAmount, const char** args) {
             coreTestPostCanary();
         }
     }
-    (void)light;
-    /*
-    Core::testArrayList(light);
-    Core::testArrayString();
-    Core::testArray();
-    Core::testBitArray();
-    Core::testBox();
-    Core::testBuffer(light);
-    Core::testBufferedValue();
-    Core::testClock(light);
-    Core::testColor();
-    Core::testComponents();
-    Core::testError();
-    Core::testFileReader();
-    Core::testFrustum();
-    Core::testHashedString();
-    Core::testHashMap(light);
-    Core::testLinkedList(light);
-    Core::testList(light);
-    Core::testMath();
-    Core::testMatrixStack(light);
-    Core::testMatrix();
-    Core::testNew();
-    Core::testPlane();
-    Core::testQuaternion();
-    Core::testRandom(light);
-    Core::testRingBuffer();
-    Core::testStack(light);
-    Core::testThread();
-    Core::testUniquePointer();*/
+    // coreTestArrayList(light);
+    // coreTestArrayString();
+    // coreTestBitArray();
+    // coreTestBox();
+    coreTestBuffer(light);
+    // coreTestBufferedValue();
+    // coreTestClock(light);
+    // coreTestColor();
+    // coreTestComponents();
+    // coreTestFileReader();
+    // coreTestFrustum();
+    // coreTestHashedString();
+    // coreTestHashMap(light);
+    // coreTestLinkedList(light);
+    // coreTestList(light);
+    // coreTestMath();
+    // coreTestMatrixStack(light);
+    // coreTestMatrix();
+    // coreTestPlane();
+    // coreTestQuaternion();
+    // coreTestRandom(light);
+    // coreTestRingBuffer();
+    // coreTestStack(light);
+    // coreTestThread();
+    // coreTestUniquePointer();
     coreTestUtility();
-    /*Core::testVector();
-    Core::testView();*/
+    // coreTestVector();
+    // coreTestView();
 
     coreLogLevel = CORE_LOG_WARNING;
     CORE_LOG_DEBUG("You won't see this!");

+ 2 - 1
test/Test.c

@@ -25,7 +25,7 @@ static Result* getResult(const char* file) {
     }
     while(resultsIndex >= resultsCapacity) {
         size_t newCapacity = resultsCapacity == 0 ? 8 : resultsCapacity * 2;
-        results = realloc(results, newCapacity);
+        results = realloc(results, newCapacity * sizeof(Result));
         if(results == nullptr) {
             printf("realloc failure in '%s' at '%d'\n", __FILE__, __LINE__);
             exit(1);
@@ -83,6 +83,7 @@ static bool addToResult(const char* file, bool comparison) {
 
 CORE_TEST_NAMED_COMPARE(Int, int, "%d")
 CORE_TEST_NAMED_COMPARE(U64, u64, "%lu")
+CORE_TEST_NAMED_COMPARE(Size, size_t, "%zu")
 CORE_TEST_NAMED_COMPARE(Bool, bool, "%d")
 
 bool coreTestString(CORE_TEST_ARGS, const char* wanted, const char* actual) {

+ 2 - 0
test/Test.h

@@ -11,6 +11,7 @@ void coreFinalizeTests(void);
 
 CORE_TEST_FUNCTION(Int, int);
 CORE_TEST_FUNCTION(U64, u64);
+CORE_TEST_FUNCTION(Size, size_t);
 CORE_TEST_FUNCTION(Bool, bool);
 CORE_TEST_FUNCTION(String, const char*);
 
@@ -28,6 +29,7 @@ bool coreTestNotNull(CORE_TEST_ARGS, const void* p);
 #define CORE_TEST_BOOL(wanted, actual) CORE_TEST(wanted, actual, Bool)
 #define CORE_TEST_INT(wanted, actual) CORE_TEST(wanted, actual, Int)
 #define CORE_TEST_U64(wanted, actual) CORE_TEST(wanted, actual, U64)
+#define CORE_TEST_SIZE(wanted, actual) CORE_TEST(wanted, actual, Size)
 #define CORE_TEST_STRING(wanted, actual) CORE_TEST(wanted, actual, String)
 #define CORE_TEST_FALSE(actual) CORE_TEST(false, actual, Bool)
 #define CORE_TEST_TRUE(actual) CORE_TEST(true, actual, Bool)

+ 0 - 3
test/Tests.h

@@ -5,7 +5,6 @@
 
 void coreTestArrayList(bool light);
 void coreTestArrayString(void);
-void coreTestArray(void);
 void coreTestBitArray(void);
 void coreTestBox(void);
 void coreTestBuffer(bool light);
@@ -13,7 +12,6 @@ void coreTestBufferedValue(void);
 void coreTestClock(bool light);
 void coreTestColor(void);
 void coreTestComponents(void);
-void coreTestError(void);
 void coreTestFileReader(void);
 void coreTestFrustum(void);
 void coreTestHashedString(void);
@@ -23,7 +21,6 @@ void coreTestList(bool light);
 void coreTestMath(void);
 void coreTestMatrixStack(bool light);
 void coreTestMatrix(void);
-void coreTestNew(void);
 void coreTestPlane(void);
 void coreTestQuaternion(void);
 void coreTestRandom(bool light);

+ 23 - 0
test/modules/BufferTests.c

@@ -0,0 +1,23 @@
+#include "../Tests.h"
+#include "core/utils/Buffer.h"
+
+static const size_t SIZE_TYPES =
+    sizeof(int) + sizeof(long) + sizeof(float) + sizeof(double);
+
+void coreTestBuffer(bool light) {
+    CoreBuffer buffer;
+    coreInitBuffer(&buffer);
+    size_t limit = light ? 1000 : 100000;
+    for(size_t i = 0; i < limit; i++) {
+        int a = 5;
+        long b = 5l;
+        float c = 5.0f;
+        double d = 5.0;
+        coreAddBufferData(&buffer, &a);
+        coreAddBufferData(&buffer, &b);
+        coreAddBufferData(&buffer, &c);
+        coreAddBufferData(&buffer, &d);
+    }
+    CORE_TEST_SIZE(SIZE_TYPES * limit, buffer.size);
+    coreDestroyBuffer(&buffer);
+}