exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
check_builtin_grammar.c
Go to the documentation of this file.
1 /*==================================================================*\
2 | EXIP - Embeddable EXI Processor in C |
3 |--------------------------------------------------------------------|
4 | This work is licensed under BSD 3-Clause License |
5 | The full license terms and conditions are located in LICENSE.txt |
6 \===================================================================*/
7 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <check.h>
21 #include "procTypes.h"
22 #include "EXISerializer.h"
23 #include "EXIParser.h"
24 #include "stringManipulate.h"
25 #include "grammarGenerator.h"
26 
27 #define INPUT_BUFFER_SIZE 200
28 #define MAX_PATH_LEN 200
29 
30 /* Location for external test data */
31 static char *dataDir;
32 
33 struct appData
34 {
35  unsigned int eventCount;
36  unsigned int attributeCount;
37 };
38 
39 
40 /* Helper functions */
41 
42 size_t readFileInputStream(void* buf, size_t readSize, void* stream)
43 {
44  FILE *infile = (FILE*) stream;
45  return fread(buf, 1, readSize, infile);
46 }
47 
48 
49 /* Document callbacks */
50 
51 static errorCode sample_fatalError(const errorCode code, const char* msg, void* app_data)
52 {
53  printf("\n%d : FATAL ERROR: %s\n", code, msg);
54  return EXIP_HANDLER_STOP;
55 }
56 
57 static errorCode sample_attribute(QName qname, void* app_data)
58 {
59  struct appData* appD = (struct appData*) app_data;
60  appD->attributeCount++;
61 
62  return EXIP_OK;
63 }
64 
65 
66 /* Tests */
67 
68 /* Basic count of document events and attributes. */
69 START_TEST (test_decode_ant_example01)
70 {
71  FILE *infile;
72  Parser testParser;
73  char buf[INPUT_BUFFER_SIZE];
74  const char *exifname = "Ant/build-build.bitPacked";
75  char exipath[MAX_PATH_LEN + strlen(exifname)];
76  struct appData parsingData;
77  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
78  BinaryBuffer buffer;
79 
80  buffer.buf = buf;
81  buffer.bufContent = 0;
82  buffer.bufLen = INPUT_BUFFER_SIZE;
83 
84  // Parsing steps:
85 
86  // I: First, define an external stream for the input to the parser if any
87  size_t pathlen = strlen(dataDir);
88  memcpy(exipath, dataDir, pathlen);
89  exipath[pathlen] = '/';
90  memcpy(&exipath[pathlen+1], exifname, strlen(exifname)+1);
91 
92  infile = fopen(exipath, "rb" );
93  if(!infile)
94  fail("Unable to open file %s", exipath);
95 
97  buffer.ioStrm.stream = infile;
98 
99  // II: Second, initialize the parser object
100  tmp_err_code = initParser(&testParser, buffer, &parsingData);
101  fail_unless (tmp_err_code == EXIP_OK, "initParser returns an error code %d", tmp_err_code);
102 
103  // III: Initialize the parsing data and hook the callback handlers to the parser object
104  parsingData.eventCount = 0;
105  parsingData.attributeCount = 0;
106 
107  testParser.handler.fatalError = sample_fatalError;
108  testParser.handler.error = sample_fatalError;
109  testParser.handler.attribute = sample_attribute;
110 
111  // IV: Parse the header of the stream
112  tmp_err_code = parseHeader(&testParser, TRUE);
113  fail_unless (tmp_err_code == EXIP_OK, "parsing the header returns an error code %d", tmp_err_code);
114  parsingData.eventCount++; // SD event is implicit
115 
116  tmp_err_code = setSchema(&testParser, NULL);
117  fail_unless (tmp_err_code == EXIP_OK, "setSchema() returns an error code %d", tmp_err_code);
118 
119  // V: Parse the body of the EXI stream
120  while(tmp_err_code == EXIP_OK)
121  {
122  tmp_err_code = parseNext(&testParser);
123  parsingData.eventCount++;
124  }
125 
126  fail_unless(parsingData.eventCount == 401,
127  "Unexpected event count: %u", parsingData.eventCount);
128  fail_unless(parsingData.attributeCount == 171,
129  "Unexpected attribute count: %u", parsingData.attributeCount);
130 
131  // VI: Free the memory allocated by the parser object
132  destroyParser(&testParser);
133  fclose(infile);
134  fail_unless (tmp_err_code == EXIP_PARSING_COMPLETE, "Error during parsing of the EXI body %d", tmp_err_code);
135 }
136 END_TEST
137 
138 
139 /* Test suite */
140 
142 {
143  Suite *s = suite_create("Built-in Grammar");
144 
145  {
146  /* Schema-less test case */
147  TCase *tc_builtin = tcase_create ("Built-in Grammar");
148  tcase_add_test (tc_builtin, test_decode_ant_example01);
149  suite_add_tcase (s, tc_builtin);
150  }
151 
152  return s;
153 }
154 
155 int main (int argc, char *argv[])
156 {
157  if (argc < 2)
158  {
159  printf("ERR: Expected test data directory\n");
160  exit(1);
161  }
162  if (strlen(argv[1]) > MAX_PATH_LEN)
163  {
164  printf("ERR: Test data pathname too long: %u", (unsigned int) strlen(argv[1]));
165  exit(1);
166  }
167  dataDir = argv[1];
168 
169  int number_failed;
170  Suite *s = exip_suite();
171  SRunner *sr = srunner_create (s);
172 #ifdef _MSC_VER
174 #endif
176  number_failed = srunner_ntests_failed (sr);
177  srunner_free (sr);
178  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
179 }
180