Added test cases for C function appendBitsToBuffer().

pull/11/head
Project Nayuki 8 years ago
parent d32b2a58fc
commit d11707d06a

@ -44,6 +44,7 @@ static int numTestCases = 0;
// Prototypes of private functions under test
int getTextProperties(const char *text, bool *isNumeric, bool *isAlphanumeric, int *textBits);
void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen);
int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl);
int getNumRawDataModules(int version);
void calcReedSolomonGenerator(int degree, uint8_t result[]);
@ -116,6 +117,47 @@ static void testGetTextProperties(void) {
}
static void testAppendBitsToBuffer(void) {
{
uint8_t buf[1] = {0};
int bitLen = 0;
appendBitsToBuffer(0, 0, buf, &bitLen);
assert(bitLen == 0);
assert(buf[0] == 0);
appendBitsToBuffer(1, 1, buf, &bitLen);
assert(bitLen == 1);
assert(buf[0] == 0x80);
appendBitsToBuffer(0, 1, buf, &bitLen);
assert(bitLen == 2);
assert(buf[0] == 0x80);
appendBitsToBuffer(5, 3, buf, &bitLen);
assert(bitLen == 5);
assert(buf[0] == 0xA8);
appendBitsToBuffer(6, 3, buf, &bitLen);
assert(bitLen == 8);
assert(buf[0] == 0xAE);
numTestCases++;
}
{
uint8_t buf[6] = {0};
int bitLen = 0;
appendBitsToBuffer(16942, 16, buf, &bitLen);
assert(bitLen == 16);
assert(buf[0] == 0x42 && buf[1] == 0x2E && buf[2] == 0x00 && buf[3] == 0x00 && buf[4] == 0x00 && buf[5] == 0x00);
appendBitsToBuffer(10, 7, buf, &bitLen);
assert(bitLen == 23);
assert(buf[0] == 0x42 && buf[1] == 0x2E && buf[2] == 0x14 && buf[3] == 0x00 && buf[4] == 0x00 && buf[5] == 0x00);
appendBitsToBuffer(15, 4, buf, &bitLen);
assert(bitLen == 27);
assert(buf[0] == 0x42 && buf[1] == 0x2E && buf[2] == 0x15 && buf[3] == 0xE0 && buf[4] == 0x00 && buf[5] == 0x00);
appendBitsToBuffer(26664, 15, buf, &bitLen);
assert(bitLen == 42);
assert(buf[0] == 0x42 && buf[1] == 0x2E && buf[2] == 0x15 && buf[3] == 0xFA && buf[4] == 0x0A && buf[5] == 0x00);
numTestCases++;
}
}
static void testGetNumDataCodewords(void) {
int cases[][3] = {
{ 3, 1, 44},
@ -449,6 +491,7 @@ static void testGetSetModuleRandomly(void) {
int main(void) {
srand(time(NULL));
testGetTextProperties();
testAppendBitsToBuffer();
testGetNumDataCodewords();
testGetNumRawDataModules();
testCalcReedSolomonGenerator();

@ -58,7 +58,7 @@ static int fitVersionToData(int minVersion, int maxVersion, enum qrcodegen_Ecc e
int dataLen, int dataBitLen, int ver1To9LenBits, int ver10To26LenBits, int ver27To40LenBits);
static void encodeQrCodeTail(uint8_t dataAndQrcode[], int bitLen, uint8_t tempBuffer[],
int version, enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, bool boostEcl);
static void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen);
testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen);
static void appendErrorCorrection(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]);
testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl);
@ -342,7 +342,7 @@ static void encodeQrCodeTail(uint8_t dataAndQrcode[], int bitLen, uint8_t tempBu
// Appends the given sequence of bits to the given byte-based bit buffer, increasing the bit length.
static void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen) {
testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen) {
assert(0 <= numBits && numBits <= 16 && (long)val >> numBits == 0);
for (int i = numBits - 1; i >= 0; i--, (*bitLen)++)
buffer[*bitLen >> 3] |= ((val >> i) & 1) << (7 - (*bitLen & 7));

Loading…
Cancel
Save