mirror of
https://github.com/status-im/QR-Code-generator.git
synced 2026-08-31 01:31:10 +00:00
Tweaked C test program sto be more C++-friendly by adding macro to cast malloc(), refactored existing code to use the macro.
This commit is contained in:
+14
-8
@@ -37,6 +37,12 @@
|
||||
|
||||
#define ARRAY_LENGTH(name) (sizeof(name) / sizeof(name[0]))
|
||||
|
||||
#ifndef __cplusplus
|
||||
#define MALLOC(num, type) malloc((num) * sizeof(type))
|
||||
#else
|
||||
#define MALLOC(num, type) static_cast<type*>(malloc((num) * sizeof(type)))
|
||||
#endif
|
||||
|
||||
|
||||
// Global variables
|
||||
static int numTestCases = 0;
|
||||
@@ -171,11 +177,11 @@ static uint8_t *appendErrorCorrectionReference(const uint8_t *data, int version,
|
||||
int shortBlockLen = rawCodewords / numBlocks;
|
||||
|
||||
// Split data into blocks and append ECC to each block
|
||||
uint8_t **blocks = malloc(numBlocks * sizeof(uint8_t*));
|
||||
uint8_t *generator = malloc(blockEccLen * sizeof(uint8_t));
|
||||
uint8_t **blocks = MALLOC(numBlocks, uint8_t*);
|
||||
uint8_t *generator = MALLOC(blockEccLen, uint8_t);
|
||||
calcReedSolomonGenerator(blockEccLen, generator);
|
||||
for (int i = 0, k = 0; i < numBlocks; i++) {
|
||||
uint8_t *block = malloc((shortBlockLen + 1) * sizeof(uint8_t));
|
||||
uint8_t *block = MALLOC(shortBlockLen + 1, uint8_t);
|
||||
int blockDataLen = shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1);
|
||||
memcpy(block, &data[k], blockDataLen * sizeof(uint8_t));
|
||||
calcReedSolomonRemainder(&data[k], blockDataLen, generator, blockEccLen, &block[shortBlockLen + 1 - blockEccLen]);
|
||||
@@ -185,7 +191,7 @@ static uint8_t *appendErrorCorrectionReference(const uint8_t *data, int version,
|
||||
free(generator);
|
||||
|
||||
// Interleave (not concatenate) the bytes from every block into a single sequence
|
||||
uint8_t *result = malloc(rawCodewords * sizeof(uint8_t));
|
||||
uint8_t *result = MALLOC(rawCodewords, uint8_t);
|
||||
for (int i = 0, k = 0; i < shortBlockLen + 1; i++) {
|
||||
for (int j = 0; j < numBlocks; j++) {
|
||||
// Skip the padding byte in short blocks
|
||||
@@ -206,15 +212,15 @@ static void testAppendErrorCorrection(void) {
|
||||
for (int version = 1; version <= 40; version++) {
|
||||
for (int ecl = 0; ecl < 4; ecl++) {
|
||||
int dataLen = getNumDataCodewords(version, (enum qrcodegen_Ecc)ecl);
|
||||
uint8_t *pureData = malloc(dataLen * sizeof(uint8_t));
|
||||
uint8_t *pureData = MALLOC(dataLen, uint8_t);
|
||||
for (int i = 0; i < dataLen; i++)
|
||||
pureData[i] = rand() % 256;
|
||||
uint8_t *expectOutput = appendErrorCorrectionReference(pureData, version, (enum qrcodegen_Ecc)ecl);
|
||||
|
||||
int dataAndEccLen = getNumRawDataModules(version) / 8;
|
||||
uint8_t *paddedData = malloc(dataAndEccLen * sizeof(uint8_t));
|
||||
uint8_t *paddedData = MALLOC(dataAndEccLen, uint8_t);
|
||||
memcpy(paddedData, pureData, dataLen * sizeof(uint8_t));
|
||||
uint8_t *actualOutput = malloc(dataAndEccLen * sizeof(uint8_t));
|
||||
uint8_t *actualOutput = MALLOC(dataAndEccLen, uint8_t);
|
||||
appendErrorCorrection(paddedData, version, (enum qrcodegen_Ecc)ecl, actualOutput);
|
||||
|
||||
assert(memcmp(actualOutput, expectOutput, dataAndEccLen * sizeof(uint8_t)) == 0);
|
||||
@@ -419,7 +425,7 @@ 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));
|
||||
uint8_t *qrcode = MALLOC(qrcodegen_BUFFER_LEN_FOR_VERSION(ver), uint8_t);
|
||||
assert(qrcode != NULL);
|
||||
initializeFunctionModules(ver, qrcode);
|
||||
|
||||
|
||||
+10
-4
@@ -33,6 +33,12 @@
|
||||
#include <stdlib.h>
|
||||
#include "qrcodegen.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
#define MALLOC(num, type) malloc((num) * sizeof(type))
|
||||
#else
|
||||
#define MALLOC(num, type) static_cast<type*>(malloc((num) * sizeof(type)))
|
||||
#endif
|
||||
|
||||
|
||||
int main(void) {
|
||||
while (true) {
|
||||
@@ -46,7 +52,7 @@ int main(void) {
|
||||
|
||||
// Read data bytes
|
||||
bool isAscii = true;
|
||||
uint8_t *data = malloc(length * sizeof(uint8_t));
|
||||
uint8_t *data = MALLOC(length, uint8_t);
|
||||
if (data == NULL) {
|
||||
perror("malloc");
|
||||
return EXIT_FAILURE;
|
||||
@@ -66,8 +72,8 @@ int main(void) {
|
||||
|
||||
// Allocate memory for QR Code
|
||||
int bufferLen = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
|
||||
uint8_t *qrcode = malloc(bufferLen * sizeof(uint8_t));
|
||||
uint8_t *tempBuffer = malloc(bufferLen * sizeof(uint8_t));
|
||||
uint8_t *qrcode = MALLOC(bufferLen, uint8_t);
|
||||
uint8_t *tempBuffer = MALLOC(bufferLen, uint8_t);
|
||||
if (qrcode == NULL || tempBuffer == NULL) {
|
||||
perror("malloc");
|
||||
return EXIT_FAILURE;
|
||||
@@ -76,7 +82,7 @@ int main(void) {
|
||||
// Try to make QR Code symbol
|
||||
bool ok;
|
||||
if (isAscii) {
|
||||
char *text = malloc((length + 1) * sizeof(char));
|
||||
char *text = MALLOC(length + 1, char);
|
||||
for (int i = 0; i < length; i++)
|
||||
text[i] = (char)data[i];
|
||||
text[length] = '\0';
|
||||
|
||||
Reference in New Issue
Block a user