Browse Source

Separate bng_test from png2bng

rexim 5 years ago
parent
commit
0090abafb1
5 changed files with 118 additions and 79 deletions
  1. 2 1
      .gitignore
  2. 12 8
      Makefile
  3. 50 1
      bng.h
  4. 54 0
      bng_test.c
  5. 0 69
      png2bng.c

+ 2 - 1
.gitignore

@@ -2,7 +2,8 @@ a.out
 prime
 png2bng
 png2bng_test
+bng_test
 bngviewer
 *.wasm
 *.bng
-bng.wat
+bng.wat

+ 12 - 8
Makefile

@@ -1,16 +1,21 @@
-BNGVIEWER_CFLAGS=$(shell pkg-config --cflags sdl2)
+BNGVIEWER_CFLAGS=$(shell pkg-config --cflags sdl2) -ggdb 
 BNGVIEWER_LIBS=$(shell pkg-config --libs sdl2)
 
+PNG2BNG_CFLAGS=-ggdb 
+
+BNG_TEST_CFLAGS=-ggdb
+
+.PHONY: all
 all: png2bng tsodinw.bng bngviewer bng.wasm
 
 png2bng: png2bng.c bng.h stb_image.h
-	$(CC) -O3 -o png2bng png2bng.c -lm
+	$(CC) $(PNG2BNG_CFLAGS) -o png2bng png2bng.c -lm
 
 tsodinw.bng: png2bng tsodinw.png
 	./png2bng tsodinw.png tsodinw.bng GRAB
 
 bngviewer: bngviewer.c bng.h
-	$(CC) $(BNGVIEWER_CFLAGS) -O3 -o bngviewer bngviewer.c $(BNGVIEWER_LIBS)
+	$(CC) $(BNGVIEWER_CFLAGS) -o bngviewer bngviewer.c $(BNGVIEWER_LIBS)
 
 bng.wasm: bng.wat
 	wat2wasm bng.wat
@@ -19,9 +24,8 @@ bng.wat: bng.in.wat
 	cpp -P bng.in.wat > bng.wat
 
 .PHONY: test
+test: bng_test
+	./bng_test
 
-test: png2bng_test
-	./png2bng_test
-
-png2bng_test: png2bng.c bng.h stb_image.h
-	$(CC) -O3 -DTEST -o png2bng_test png2bng.c -lm
+bng_test: bng_test.c bng.h stb_image.h
+	$(CC) $(BNG_TEST_CFLAGS) -o bng_test bng_test.c -lm

+ 50 - 1
bng.h

@@ -6,7 +6,8 @@
 #if defined(__GNUC__) || defined(__clang__)
 #  define PACKED __attribute__((packed))
 #else
-#  warning "Packed attributes for struct is not implemented for this compiler. Feel free to fix that and submit a Pull Request to https://github.com/tsoding/bng"
+#  warning "Packed attributes for struct is not implemented for this compiler. This may result in a program working incorrectly. Feel free to fix that and submit a Pull Request to https://github.com/tsoding/bng"
+#  define PACKED
 #endif
 
 struct Bng_Pixel_Format
@@ -29,6 +30,54 @@ const struct Bng_Pixel_Format RGBA = {0, 1, 2, 3};
 const struct Bng_Pixel_Format ABGR = {3, 2, 1, 0};
 const struct Bng_Pixel_Format GRAB = {1, 0, 3, 2};
 
+struct Bng_Pixel_Format_Name
+{
+    char cstr[5];
+};
+
+struct Bng_Pixel_Format_Name name_of_pixel_format(struct Bng_Pixel_Format format)
+{
+    struct Bng_Pixel_Format_Name name = {0};
+    // BTW: if sizeof(Bng_Pixel_Format) was 1 I would not have to do these asserts, because then
+    // the values of byte indices could not be physically equal or greater than 4.
+    assert(format.red_byte < 4);
+    name.cstr[format.red_byte] = 'R';
+    assert(format.green_byte < 4);
+    name.cstr[format.green_byte] = 'G';
+    assert(format.blue_byte < 4);
+    name.cstr[format.blue_byte] = 'B';
+    assert(format.alpha_byte < 4);
+    name.cstr[format.alpha_byte] = 'A';
+    return name;
+}
+
+struct Bng_Pixel_Format pixel_format_by_name(const char *name)
+{
+    struct Bng_Pixel_Format result = {0};
+    size_t n = strlen(name);
+    if (n > 4) n = 4;
+
+    for (size_t i = 0; i < n; ++i) {
+        switch (name[i]) {
+        case 'R':
+            result.red_byte = i;
+            break;
+        case 'G':
+            result.green_byte = i;
+            break;
+        case 'B':
+            result.blue_byte = i;
+            break;
+        case 'A':
+            result.alpha_byte = i;
+            break;
+        default: {}
+        }
+    }
+
+    return result;
+}
+
 struct Bng
 {
     uint32_t magic;

+ 54 - 0
bng_test.c

@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+#include "bng.h"
+
+void test_convert_pixel()
+{
+#define TEST_CASE(input, source, desired, expected)                 \
+    do {                                                            \
+        uint32_t actual = convert_pixel(input, source, desired);    \
+        if (actual != expected) {                                   \
+            printf("%s:%d: Oopsie-doopsie!\n", __FILE__, __LINE__); \
+            printf("Actual: %08X\n", actual);                       \
+            printf("Expected: %08X\n", expected);                   \
+            exit(1);                                                \
+        }                                                           \
+        printf("%08X == %08X, OK\n", actual, expected);             \
+    } while (0)
+
+    TEST_CASE(0xAABBCCDD, RGBA, ABGR, 0xDDCCBBAA);
+    TEST_CASE(0xAABBCCDD, ABGR, GRAB, 0xCCDDAABB);
+    TEST_CASE(0xAABBCCDD, RGBA, GRAB, 0xBBAADDCC);
+#undef TEST_CASE
+}
+
+void test_pixel_format_by_name()
+{
+#define TEST_CASE(input, expected)\
+    do {\
+        const struct Bng_Pixel_Format actual = pixel_format_by_name(input);\
+        if (!bng_pixel_format_equals(expected, actual)) {\
+            fprintf(stderr, "%s:%d: Oopsie-doopsie!\n", __FILE__, __LINE__);\
+            fprintf(stderr, "Actual: %s\n", name_of_pixel_format(actual));\
+            fprintf(stderr, "Expected: %s\n", name_of_pixel_format(expected));\
+            exit(1);\
+        }\
+        printf("%s == %s, OK\n", name_of_pixel_format(actual).cstr, name_of_pixel_format(expected).cstr);\
+    } while(0);
+
+    TEST_CASE("RGBA", RGBA);
+    TEST_CASE("ABGR", ABGR);
+    TEST_CASE("GRAB", GRAB);
+#undef TEST_CASE
+}
+
+int main(int argc, char *argv[])
+{
+    test_convert_pixel();
+    test_pixel_format_by_name();
+    return 0;
+}
+

+ 0 - 69
png2bng.c

@@ -2,74 +2,6 @@
 #include "./stb_image.h"
 #include "./bng.h"
 
-
-
-
-struct Bng_Pixel_Format pixel_format_by_name(const char *name)
-{
-    struct Bng_Pixel_Format result = {0};
-    size_t n = strlen(name);
-    if (n > 4) n = 4;
-
-    for (size_t i = 0; i < n; ++i) {
-        switch (name[i]) {
-        case 'R':
-            result.red_byte = i;
-            break;
-        case 'G':
-            result.green_byte = i;
-            break;
-        case 'B':
-            result.blue_byte = i;
-            break;
-        case 'A':
-            result.alpha_byte = i;
-            break;
-        default: {}
-        }
-    }
-
-    return result;
-}
-
-#ifdef TEST
-int test_convert_pixel()
-{
-#define TEST_CASE(input, source, desired, expected)                 \
-    do {                                                            \
-        uint32_t actual = convert_pixel(input, source, desired);    \
-        if (actual != expected) {                                   \
-            printf("%s:%d: Oopsie-doopsie!\n", __FILE__, __LINE__); \
-            printf("Actual: %08X\n", actual);                       \
-            printf("Expected: %08X\n", expected);                   \
-            exit(1);                                                \
-        }                                                           \
-        printf("%08X == %08X, OK\n", actual, expected);             \
-    } while (0)
-
-    TEST_CASE(0xAABBCCDD, RGBA, ABGR, 0xDDCCBBAA);
-    TEST_CASE(0xAABBCCDD, ABGR, GRAB, 0xCCDDAABB);
-    TEST_CASE(0xAABBCCDD, RGBA, GRAB, 0xBBAADDCC);
-#undef TEST_CASE
-}
-
-int test_pixel_format_by_name()
-{
-    assert(bng_pixel_format_equals(pixel_format_by_name("RGBA"), RGBA));
-    assert(bng_pixel_format_equals(pixel_format_by_name("ABGR"), ABGR));
-    assert(bng_pixel_format_equals(pixel_format_by_name("GRAB"), GRAB));
-    return 0;
-}
-
-int main(int argc, char *argv[])
-{
-    test_convert_pixel();
-    test_pixel_format_by_name();
-    return 0;
-}
-
-#else
-
 int main(int argc, char **argv)
 {
     if (argc < 4) {
@@ -103,4 +35,3 @@ int main(int argc, char **argv)
 
     return 0;
 }
-#endif