Kajetan Johannes Hammerle 1 жил өмнө
parent
commit
f977972c0d

+ 1 - 1
include/core/utils/ArrayString.hpp

@@ -18,7 +18,7 @@ namespace Core {
                 return s.append(static_cast<const char*>(t));
             } else {
                 char buffer[64];
-                CORE_RETURN_ERROR(toString(t, buffer, CORE_SIZE(buffer)));
+                toString(t, buffer, CORE_SIZE(buffer));
                 return s.append(static_cast<const char*>(buffer));
             }
         }

+ 1 - 5
include/core/utils/Error.hpp

@@ -38,10 +38,6 @@ namespace Core {
             e |= *this;
             return e;
         }
-
-        operator Code() const {
-            return code;
-        }
     };
 
     namespace ErrorCode {
@@ -65,7 +61,7 @@ namespace Core {
         static constexpr Error END_OF_FILE = 1 << 16;
     }
 
-    CError toString(Error e, char* buffer, size_t size);
+    size_t toString(Error e, char* buffer, size_t size);
 
     inline bool checkError(Error& storage, Error e) {
         return (storage = e).check();

+ 11 - 11
include/core/utils/Utility.hpp

@@ -25,17 +25,17 @@ namespace Core {
 #define CORE_EXIT(exitValue)                                                   \
     Core::exitWithHandler(__FILE__, __LINE__, exitValue)
 
-    CError toString(signed short s, char* buffer, size_t size);
-    CError toString(unsigned short s, char* buffer, size_t size);
-    CError toString(signed int i, char* buffer, size_t size);
-    CError toString(unsigned int i, char* buffer, size_t size);
-    CError toString(signed long l, char* buffer, size_t size);
-    CError toString(unsigned long l, char* buffer, size_t size);
-    CError toString(signed long long ll, char* buffer, size_t size);
-    CError toString(unsigned long long ll, char* buffer, size_t size);
-    CError toString(float f, char* buffer, size_t size);
-    CError toString(double d, char* buffer, size_t size);
-    CError toString(long double ld, char* buffer, size_t size);
+    size_t toString(signed short s, char* buffer, size_t size);
+    size_t toString(unsigned short s, char* buffer, size_t size);
+    size_t toString(signed int i, char* buffer, size_t size);
+    size_t toString(unsigned int i, char* buffer, size_t size);
+    size_t toString(signed long l, char* buffer, size_t size);
+    size_t toString(unsigned long l, char* buffer, size_t size);
+    size_t toString(signed long long ll, char* buffer, size_t size);
+    size_t toString(unsigned long long ll, char* buffer, size_t size);
+    size_t toString(float f, char* buffer, size_t size);
+    size_t toString(double d, char* buffer, size_t size);
+    size_t toString(long double ld, char* buffer, size_t size);
 
     void print(int c);
     void print(const char* s);

+ 11 - 14
src/Error.cpp

@@ -1,23 +1,20 @@
 #include "core/utils/Error.hpp"
 
-CError Core::toString(Error e, char* buffer, size_t size) {
-    if(size <= 0) {
-        return ErrorCode::CAPACITY_REACHED;
-    }
-    size_t index = 0;
+size_t Core::toString(Error e, char* buffer, size_t size) {
+    size_t written = 0;
     Error::Code c = e.code;
-    size--;
-    while(true) {
-        if(index >= size) {
-            buffer[index] = '\0';
-            return ErrorCode::CAPACITY_REACHED;
-        }
-        buffer[index++] = (c & 1) ? '1' : '0';
+    for(size_t i = size; i > 1; i--) {
+        *(buffer++) = (c & 1) ? '1' : '0';
+        written++;
         c >>= 1;
         if(c == 0) {
             break;
         }
     }
-    buffer[index] = '\0';
-    return ErrorCode::NONE;
+    *buffer = '\0';
+    while(c != 0) {
+        written++;
+        c >>= 1;
+    }
+    return written;
 }

+ 7 - 11
src/Utility.cpp

@@ -31,13 +31,9 @@ void Core::setExitHandler(ExitHandler eh, void* data) {
 }
 
 #define CORE_TO_STRING(type, cast, format)                                     \
-    CError Core::toString(type t, char* buffer, size_t size) {                 \
+    size_t Core::toString(type t, char* buffer, size_t size) {                 \
         int w = snprintf(buffer, size, format, static_cast<cast>(t));          \
-        if(w < 0) {                                                            \
-            return ErrorCode::ERROR;                                           \
-        }                                                                      \
-        return static_cast<size_t>(w) >= size ? ErrorCode::CAPACITY_REACHED    \
-                                              : ErrorCode::NONE;               \
+        return w < 0 ? 0 : static_cast<size_t>(w);                             \
     }
 
 CORE_TO_STRING(signed short, signed short, "%hd")
@@ -54,19 +50,19 @@ CORE_TO_STRING(long double, long double, "%.2Lf")
 
 void Core::print(int c) {
     if(putchar(c) < 0) {
-        CORE_EXIT(static_cast<int>(ErrorCode::BLOCKED_STDOUT));
+        CORE_EXIT(ErrorCode::BLOCKED_STDOUT.code); // CoverageIgnore
     }
 }
 
 void Core::print(const char* s) {
     if(fputs(s, stdout) < 0) {
-        CORE_EXIT(static_cast<int>(ErrorCode::BLOCKED_STDOUT));
+        CORE_EXIT(ErrorCode::BLOCKED_STDOUT.code); // CoverageIgnore
     }
 }
 
 void Core::printLine(const char* s) {
     if(puts(s) < 0) {
-        CORE_EXIT(static_cast<int>(ErrorCode::BLOCKED_STDOUT));
+        CORE_EXIT(ErrorCode::BLOCKED_STDOUT.code); // CoverageIgnore
     }
 }
 
@@ -88,7 +84,7 @@ void* Core::allocate(size_t n) {
         p = malloc(n);
     }
     if(p == nullptr) {
-        CORE_EXIT(static_cast<int>(ErrorCode::OUT_OF_MEMORY));
+        CORE_EXIT(ErrorCode::OUT_OF_MEMORY.code); // CoverageIgnore
     }
     return p;
 }
@@ -113,7 +109,7 @@ void* Core::reallocate(void* oldP, size_t n) {
         }
     }
     if(p == nullptr) {
-        CORE_EXIT(static_cast<int>(ErrorCode::OUT_OF_MEMORY));
+        CORE_EXIT(ErrorCode::OUT_OF_MEMORY.code); // CoverageIgnore
     }
     return p;
 }

+ 15 - 0
test/modules/ArrayStringTests.cpp

@@ -1,6 +1,7 @@
 #include "../Tests.hpp"
 #include "core/data/HashMap.hpp"
 #include "core/utils/ArrayString.hpp"
+#include "core/utils/Error.hpp"
 
 template class Core::ArrayString<128, char, Core::CharString>;
 template class Core::ArrayString<128, c32, Core::Char32String>;
@@ -459,6 +460,12 @@ static void testUnicodeString8() {
     CORE_TEST_STRING("_üö§äab", s);
 }
 
+static void testInvalidUnicodeString8() {
+    String8 s;
+    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_CHAR,
+                    s.append(static_cast<c32>(0xFFFFFFFF)));
+}
+
 static String32 build(const c32* cs) {
     String32 s;
     CORE_TEST_ERROR(s.append(cs));
@@ -924,6 +931,12 @@ static void testKeepHash32() {
     CORE_TEST_STRING("a # test ## ##123456789", s);
 }
 
+static void testInvalidPrint32() {
+    String32 s;
+    CORE_TEST_ERROR(s.append(static_cast<c32>(0xFFFFFFFF)));
+    s.printLine();
+}
+
 static void testConversion() {
     const c32* a = U"öüewfde_§$§%$ädsf";
     const char* b = "öüewfde_§$§%$ädsf";
@@ -986,6 +999,7 @@ void Core::testArrayString() {
     testKeepHash8();
     testFormatWithoutArguments8();
     testUnicodeString8();
+    testInvalidUnicodeString8();
 
     testEquality32();
     testUnicodeEquality32();
@@ -1029,6 +1043,7 @@ void Core::testArrayString() {
     testPrint32();
     testVariousUnicode32();
     testKeepHash32();
+    testInvalidPrint32();
 
     testConversion();
 }

+ 1 - 2
test/modules/ErrorTests.cpp

@@ -35,7 +35,6 @@ void Core::testError() {
 
     char buffer[4];
     CORE_TEST_EQUAL(
-        ErrorCode::CAPACITY_REACHED,
-        Core::toString(ErrorCode::INVALID_STATE, buffer, sizeof(buffer)));
+        8, Core::toString(ErrorCode::INVALID_STATE, buffer, sizeof(buffer)));
     CORE_TEST_STRING("000", buffer);
 }

+ 16 - 0
test/modules/RandomTests.cpp

@@ -80,6 +80,21 @@ static void testFloatDistribution(bool light) {
     }
 }
 
+static void testRandomI32() {
+    Core::Random r(56346);
+    Core::Array<int, 7> c;
+    c.fill(0);
+    for(int i = 0; i < 10'000; i++) {
+        i32 index = r.nextI32(-2, 2) + 3;
+        if(CORE_TEST_TRUE(index >= 0)) {
+            c[static_cast<size_t>(index)]++;
+        }
+    }
+    for(size_t i = 0; i < c.getLength(); i++) {
+        CORE_TEST_EQUAL(i != 0 && i != c.getLength() - 1, c[i] > 0);
+    }
+}
+
 void Core::testRandom(bool light) {
     testAverage(light);
     testCoin(light);
@@ -87,4 +102,5 @@ void Core::testRandom(bool light) {
     testFloatAverage(light);
     testFloatCoin(light);
     testFloatDistribution(light);
+    testRandomI32();
 }