test/lexer.c
00001 #include <u/libu.h>
00002
00003 int test_suite_lexer_register (u_test_t *t);
00004
00005 static int test_scan0 (u_test_case_t *tc);
00006 static int test_scan1 (u_test_case_t *tc);
00007 static int test_match (u_test_case_t *tc);
00008
00009 static int scan (u_test_case_t *tc, int (*f)(u_lexer_t *, char *));
00010
00011
00012 static int scan (u_test_case_t *tc, int (*f)(u_lexer_t *, char *))
00013 {
00014 char c;
00015 int i = 0;
00016 u_lexer_t *l = NULL;
00017 const char *s = "abc AB\tC\n1 2 3 ";
00018 const char *ex0 = "abcABC123", *ex1 = s;
00019 char dest[1024] = { '\0' };
00020
00021 u_test_err_if (u_lexer_new(s, &l));
00022
00023
00024 c = u_lexer_peek(l);
00025 dest[i] = c;
00026
00027 while (f(l, &c) != -1)
00028 dest[++i] = c;
00029
00030 dest[++i] = '\0';
00031
00032 if (f == u_lexer_next)
00033 u_test_err_if (strcmp(dest, ex1));
00034 else if (f == u_lexer_skip)
00035 u_test_err_if (strcmp(dest, ex0));
00036 else
00037 u_test_err_ifm (1, "uh?");
00038
00039 u_lexer_free(l);
00040 return U_TEST_SUCCESS;
00041 err:
00042 u_lexer_free(l);
00043 return U_TEST_FAILURE;
00044 }
00045
00046 static int test_scan0 (u_test_case_t *tc)
00047 {
00048 return scan(tc, u_lexer_next);
00049 }
00050
00051 static int test_scan1 (u_test_case_t *tc)
00052 {
00053 return scan(tc, u_lexer_skip);
00054 }
00055
00056
00057 static int test_match (u_test_case_t *tc)
00058 {
00059 char c;
00060 u_lexer_t *l = NULL;
00061 char match[U_TOKEN_SZ];
00062 #define EXP "*match me*"
00063 const char *s = "abc " EXP " ABC";
00064
00065 u_test_err_if (u_lexer_new(s, &l));
00066
00067
00068 while (u_lexer_next(l, &c) != -1)
00069 {
00070 if (c == '*')
00071 {
00072 u_lexer_record_lmatch(l);
00073 break;
00074 }
00075 }
00076
00077
00078 while (u_lexer_next(l, &c) != -1)
00079 {
00080 if (c == '*')
00081 {
00082 u_lexer_record_rmatch(l);
00083 break;
00084 }
00085 }
00086
00087 u_test_err_if (strcmp(u_lexer_get_match(l, match), EXP));
00088 #undef EXP
00089 u_lexer_free(l);
00090 return U_TEST_SUCCESS;
00091 err:
00092 u_lexer_free(l);
00093 return U_TEST_FAILURE;
00094 }
00095
00096 int test_suite_lexer_register (u_test_t *t)
00097 {
00098 u_test_suite_t *ts = NULL;
00099
00100 con_err_if (u_test_suite_new("Lexer", &ts));
00101
00102 con_err_if (u_test_case_register("scan (no skip ws)", test_scan0, ts));
00103 con_err_if (u_test_case_register("scan (skip ws)", test_scan1, ts));
00104 con_err_if (u_test_case_register("match", test_match, ts));
00105
00106 return u_test_suite_add(ts, t);
00107 err:
00108 u_test_suite_free(ts);
00109 return ~0;
00110 }