From 0bd768419868700d0b8b15813bab22166c0d1e3c Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Wed, 26 Apr 2017 16:44:41 +0000 Subject: [PATCH] Added C test case for initializeFunctionModules(). --- c/qrcodegen-test.c | 34 ++++++++++++++++++++++++++++++++++ c/qrcodegen.c | 4 ++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/c/qrcodegen-test.c b/c/qrcodegen-test.c index ebece33..b8c54b8 100644 --- a/c/qrcodegen-test.c +++ b/c/qrcodegen-test.c @@ -48,6 +48,7 @@ int getNumRawDataModules(int version); void calcReedSolomonGenerator(int degree, uint8_t result[]); void calcReedSolomonRemainder(const uint8_t data[], int dataLen, const uint8_t generator[], int degree, uint8_t result[]); uint8_t finiteFieldMultiply(uint8_t x, uint8_t y); +void initializeFunctionModules(int version, uint8_t qrcode[]); int getAlignmentPatternPositions(int version, uint8_t result[7]); @@ -300,6 +301,38 @@ static void testFiniteFieldMultiply(void) { } +static void testInitializeFunctionModulesEtc(void) { + for (int ver = 1; ver <= 40; ver++) { + uint8_t *qrcode = malloc(qrcodegen_BUFFER_LEN_FOR_VERSION(ver) * sizeof(uint8_t)); + assert(qrcode != NULL); + initializeFunctionModules(ver, qrcode); + + int size = qrcodegen_getSize(qrcode); + if (ver == 1) + assert(size == 21); + else if (ver == 40) + assert(size == 177); + else + assert(size == ver * 4 + 17); + + bool hasWhite = false; + bool hasBlack = false; + for (int y = 0; y < size; y++) { + for (int x = 0; x < size; x++) { + bool color = qrcodegen_getModule(qrcode, x, y); + if (color) + hasBlack = true; + else + hasWhite = true; + } + } + assert(hasWhite && hasBlack); + free(qrcode); + numTestCases++; + } +} + + static void testGetAlignmentPatternPositions(void) { int cases[][9] = { { 1, 0, -1, -1, -1, -1, -1, -1, -1}, @@ -336,6 +369,7 @@ int main(void) { testCalcReedSolomonGenerator(); testCalcReedSolomonRemainder(); testFiniteFieldMultiply(); + testInitializeFunctionModulesEtc(); testGetAlignmentPatternPositions(); printf("All %d test cases passed\n", numTestCases); return EXIT_SUCCESS; diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 33fb7d9..b02d8fb 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -56,7 +56,7 @@ testable void calcReedSolomonGenerator(int degree, uint8_t result[]); testable void calcReedSolomonRemainder(const uint8_t data[], int dataLen, const uint8_t generator[], int degree, uint8_t result[]); testable uint8_t finiteFieldMultiply(uint8_t x, uint8_t y); -static void initializeFunctionModules(int version, uint8_t qrcode[]); +testable void initializeFunctionModules(int version, uint8_t qrcode[]); static void drawWhiteFunctionModules(uint8_t qrcode[], int version); static void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uint8_t qrcode[]); testable int getAlignmentPatternPositions(int version, uint8_t result[7]); @@ -464,7 +464,7 @@ testable uint8_t finiteFieldMultiply(uint8_t x, uint8_t y) { // Clears the given QR Code grid with white modules for the given // version's size, then marks every function module as black. -static void initializeFunctionModules(int version, uint8_t qrcode[]) { +testable void initializeFunctionModules(int version, uint8_t qrcode[]) { // Initialize QR Code int qrsize = version * 4 + 17; memset(qrcode, 0, ((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0]));