Skip to content

Commit

Permalink
Exploring the JSON like wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
tamc committed Jun 16, 2016
1 parent 31a5d13 commit 3468a9f
Show file tree
Hide file tree
Showing 3 changed files with 427 additions and 8 deletions.
4 changes: 2 additions & 2 deletions util/c_wrapper/Makefile
@@ -1,7 +1,7 @@
example: example.c
clang example.c -o example
clang -g example.c -o example

example.c: parser.c excelspreadsheet.c
example.c: parser.c excelspreadsheet.c manualparser.c
touch example.c

parser.c: parser.rl
Expand Down
107 changes: 101 additions & 6 deletions util/c_wrapper/example.c
@@ -1,10 +1,105 @@
#include "excelspreadsheet.c"
#include "parser.c"
#include "manualparser.c"

int main(int argc, char *argv[]) {
if(argc > 1) {
ExcelValue result = parse(argv[1]);
set_sheet_two_a1(result);
void fail(ExcelValue actual, ExcelValue expected) {
printf("Expected: ");
inspect_excel_value(expected);
printf("Actual: ");
inspect_excel_value(actual);
exit(-1);
}


void test(ExcelValue actual, ExcelValue expected) {
int i, j, k;
ExcelValue *actual_array, *expected_array;

if(actual.type != expected.type) { fail(actual, expected); }
switch(expected.type) {
case ExcelNumber:
case ExcelBoolean:
case ExcelEmpty:
case ExcelError:
if(actual.number != expected.number) { fail(actual, expected); }
break;
case ExcelString:
if(strcasecmp(actual.string,expected.string) != 0 ) { fail(actual, expected); }
break;
case ExcelRange:
if(actual.rows != expected.rows) { fail(actual, expected); }
if(actual.columns != expected.columns) { fail(actual, expected); }
actual_array = actual.array;
expected_array = expected.array;
for(i = 0; i < actual.rows; i++) {
for(j = 0; j < actual.columns; j++) {
k = (i * expected.columns) + j;
test(actual_array[k], expected_array[k]);
}
}
break;
default:
printf("Type %d not recognised", expected.type);
exit(-1);
}
inspect_excel_value(out());
}

void testRoundTrip(ExcelValue value) {
char *string = excelValueToString(value);
printf("String '%s'\n", string);
ExcelValue parsedValue = parseValue(string);
test(parsedValue, value);
}

int main() {
//test(parseValue(" 1.23"), EXCEL_NUMBER(1.23));
//test(parseValue("-1.23"), EXCEL_NUMBER(-1.23));
//test(parseValue("-0.23e-3"), EXCEL_NUMBER(-2.3e-4));
//test(parseValue("\n\"Hello world\""), EXCEL_STRING("Hello world"));
////test(parValuese("\"Hello\\\"world\""), EXCEL_STRING("Hello\"world"));
//test(parseValue("true"), TRUE);
//test(parseValue("false"), FALSE);
//test(parseValue("#VALUE!"), VALUE);
//test(parseValue("#NAME?"), NAME);
//test(parseValue("#DIV/0!"), DIV0);
//test(parseValue("#N/A"), NA);
//test(parseValue("#NUM!"), NUM);
ExcelValue range[] = { ONE, TWO, THREE };
test(parseValue("[1,2,3]"), EXCEL_RANGE(range, 1, 3));
ExcelValue emptyRange[] = {};
test(parseValue("[]"), EXCEL_RANGE(emptyRange, 0, 0));
ExcelValue manyTypedRange[] = { ONE, TRUE, THREE, EXCEL_STRING("Four") };
test(parseValue("[1,true,3,\"Four\"]"), EXCEL_RANGE(manyTypedRange, 1, 4));
test(parseValue("[[]]"), EXCEL_RANGE(emptyRange, 1, 0));
ExcelValue columnRange[] = { ONE, TWO, THREE };
test(parseValue("[[1],[2],[3]]"), EXCEL_RANGE(columnRange, 3, 1));
ExcelValue matrixRange[] = { ONE, TWO, THREE, FOUR };
test(parseValue("[[1,2],[3,4]]"), EXCEL_RANGE(matrixRange, 2, 2));

//// Test some error modes
//test(parseValue("[\"Unterminated array\",2"), VALUE);
//test(parseValue("\"Unterminated string"), VALUE);
//test(parseValue("falt"), VALUE);
//test(parseValue("#NAA"), VALUE);
//test(parseValue("[1,[1,2]]"), VALUE);
//test(parseValue("[[[1],[2]]]"), VALUE);

// Test the output of excelValueToString
testRoundTrip(NA);
testRoundTrip(VALUE);
testRoundTrip(NUM);
testRoundTrip(DIV0);
testRoundTrip(TRUE);
testRoundTrip(FALSE);
testRoundTrip(TRUE);
testRoundTrip(EXCEL_NUMBER(1));
testRoundTrip(EXCEL_NUMBER(1.231241));
testRoundTrip(EXCEL_STRING("Hello world!"));
testRoundTrip(EXCEL_STRING(""));
testRoundTrip(EXCEL_RANGE(range, 1, 3));
testRoundTrip(EXCEL_RANGE(emptyRange, 0, 0));
testRoundTrip(EXCEL_RANGE(columnRange, 3, 1));
testRoundTrip(EXCEL_RANGE(matrixRange, 2, 2));


printf("All tests passed\n");
};

0 comments on commit 3468a9f

Please sign in to comment.