From 13c136db8bf903b0e8e6158d09d9402ba07f7d9c Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Sat, 9 Sep 2017 05:27:18 +0000 Subject: [PATCH] Made C function getTotalBits() testable, added test cases. --- c/qrcodegen-test.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++ c/qrcodegen.c | 4 +-- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/c/qrcodegen-test.c b/c/qrcodegen-test.c index 3375985..4f9c5c0 100644 --- a/c/qrcodegen-test.c +++ b/c/qrcodegen-test.c @@ -64,6 +64,7 @@ bool getModule(const uint8_t qrcode[], int x, int y); void setModule(uint8_t qrcode[], int x, int y, bool isBlack); void setModuleBounded(uint8_t qrcode[], int x, int y, bool isBlack); int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars); +int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version); /*---- Test cases ----*/ @@ -970,6 +971,87 @@ static void testMakeEci(void) { } +static void testGetTotalBits(void) { + { + assert(getTotalBits(NULL, 0, 1) == 0); + numTestCases++; + assert(getTotalBits(NULL, 0, 40) == 0); + numTestCases++; + } + { + struct qrcodegen_Segment segs[] = { + {qrcodegen_Mode_BYTE, 3, NULL, 24}, + }; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 2) == 36); + numTestCases++; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 10) == 44); + numTestCases++; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 39) == 44); + numTestCases++; + } + { + struct qrcodegen_Segment segs[] = { + {qrcodegen_Mode_ECI, 0, NULL, 8}, + {qrcodegen_Mode_NUMERIC, 7, NULL, 24}, + {qrcodegen_Mode_ALPHANUMERIC, 1, NULL, 6}, + {qrcodegen_Mode_KANJI, 4, NULL, 52}, + }; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 9) == 133); + numTestCases++; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 21) == 139); + numTestCases++; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 27) == 145); + numTestCases++; + } + { + struct qrcodegen_Segment segs[] = { + {qrcodegen_Mode_BYTE, 4093, NULL, 32744}, + }; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 1) == -1); + numTestCases++; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 10) == 32764); + numTestCases++; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 27) == 32764); + numTestCases++; + } + { + struct qrcodegen_Segment segs[] = { + {qrcodegen_Mode_NUMERIC, 2047, NULL, 6824}, + {qrcodegen_Mode_NUMERIC, 2047, NULL, 6824}, + {qrcodegen_Mode_NUMERIC, 2047, NULL, 6824}, + {qrcodegen_Mode_NUMERIC, 2047, NULL, 6824}, + {qrcodegen_Mode_NUMERIC, 1617, NULL, 5390}, + }; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 1) == -1); + numTestCases++; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 10) == 32766); + numTestCases++; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 27) == -1); + numTestCases++; + } + { + struct qrcodegen_Segment segs[] = { + {qrcodegen_Mode_KANJI, 255, NULL, 3315}, + {qrcodegen_Mode_KANJI, 255, NULL, 3315}, + {qrcodegen_Mode_KANJI, 255, NULL, 3315}, + {qrcodegen_Mode_KANJI, 255, NULL, 3315}, + {qrcodegen_Mode_KANJI, 255, NULL, 3315}, + {qrcodegen_Mode_KANJI, 255, NULL, 3315}, + {qrcodegen_Mode_KANJI, 255, NULL, 3315}, + {qrcodegen_Mode_KANJI, 255, NULL, 3315}, + {qrcodegen_Mode_KANJI, 255, NULL, 3315}, + {qrcodegen_Mode_ALPHANUMERIC, 511, NULL, 2811}, + }; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 9) == 32767); + numTestCases++; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 26) == -1); + numTestCases++; + assert(getTotalBits(segs, ARRAY_LENGTH(segs), 40) == -1); + numTestCases++; + } +} + + /*---- Main runner ----*/ int main(void) { @@ -993,6 +1075,7 @@ int main(void) { testMakeNumeric(); testMakeAlphanumeric(); testMakeEci(); + testGetTotalBits(); printf("All %d test cases passed\n", numTestCases); return EXIT_SUCCESS; } diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 54d284c..bfae0e8 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -84,7 +84,7 @@ testable void setModule(uint8_t qrcode[], int x, int y, bool isBlack); testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isBlack); testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars); -static int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version); +testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version); static int numCharCountBits(enum qrcodegen_Mode mode, int version); @@ -965,7 +965,7 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz } -static int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version) { +testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version) { assert(segs != NULL || len == 0); assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX); int result = 0;