Added C functions to test whether strings can be encoded in certain segment modes, added test cases.

pull/18/head
Project Nayuki 8 years ago
parent 08108ee6d8
commit ec24c428ba

@ -563,6 +563,72 @@ static void testGetSetModuleRandomly(void) {
}
static void testIsAlphanumeric(void) {
struct TestCase {
bool answer;
const char *text;
};
const struct TestCase cases[] = {
{true, ""},
{true, "0"},
{true, "A"},
{false, "a"},
{true, " "},
{true, "."},
{true, "*"},
{false, ","},
{false, "|"},
{false, "@"},
{true, "XYZ"},
{false, "XYZ!"},
{true, "79068"},
{true, "+123 ABC$"},
{false, "\x01"},
{false, "\x7F"},
{false, "\x80"},
{false, "\xC0"},
{false, "\xFF"},
};
for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {
assert(qrcodegen_isAlphanumeric(cases[i].text) == cases[i].answer);
numTestCases++;
}
}
static void testIsNumeric(void) {
struct TestCase {
bool answer;
const char *text;
};
const struct TestCase cases[] = {
{true, ""},
{true, "0"},
{false, "A"},
{false, "a"},
{false, " "},
{false, "."},
{false, "*"},
{false, ","},
{false, "|"},
{false, "@"},
{false, "XYZ"},
{false, "XYZ!"},
{true, "79068"},
{false, "+123 ABC$"},
{false, "\x01"},
{false, "\x7F"},
{false, "\x80"},
{false, "\xC0"},
{false, "\xFF"},
};
for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {
assert(qrcodegen_isNumeric(cases[i].text) == cases[i].answer);
numTestCases++;
}
}
static void testCalcSegmentBufferSize(void) {
{
const size_t cases[][2] = {
@ -830,6 +896,8 @@ int main(void) {
testGetAlignmentPatternPositions();
testGetSetModule();
testGetSetModuleRandomly();
testIsAlphanumeric();
testIsNumeric();
testCalcSegmentBufferSize();
testCalcSegmentBitLength();
printf("All %d test cases passed\n", numTestCases);

@ -856,6 +856,26 @@ testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isBlack) {
/*---- Segment handling ----*/
bool qrcodegen_isAlphanumeric(const char *text) {
assert(text != NULL);
for (; *text != '\0'; text++) {
if (strchr(ALPHANUMERIC_CHARSET, *text) == NULL)
return false;
}
return true;
}
bool qrcodegen_isNumeric(const char *text) {
assert(text != NULL);
for (; *text != '\0'; text++) {
if (*text < '0' || *text > '9')
return false;
}
return true;
}
size_t qrcodegen_calcSegmentBufferSize(enum qrcodegen_Mode mode, size_t numChars) {
int temp = calcSegmentBitLength(mode, numChars);
if (temp == -1)

@ -153,6 +153,18 @@ bool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcod
enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl);
/*
* Tests whether the given string can be encoded as a segment in alphanumeric mode.
*/
bool qrcodegen_isAlphanumeric(const char *text);
/*
* Tests whether the given string can be encoded as a segment in numeric mode.
*/
bool qrcodegen_isNumeric(const char *text);
/*
* Returns the number of bytes (uint8_t) needed for the data buffer of a segment
* containing the given number of characters using the given mode. Notes:

Loading…
Cancel
Save