mumble

A Lisp written in C, following the *Build Your Own Lisp* book
Log | Files | Refs | README

combinators.c (2839B)


      1 #include "ptest.h"
      2 #include "../mpc.h"
      3 
      4 static int check_is_a(mpc_val_t** x) {
      5   return strcmp(*x, "a") == 0;
      6 }
      7 
      8 static int check_is(mpc_val_t** x, void* t) {
      9   return strcmp(*x, t) == 0;
     10 }
     11 
     12 void test_check(void) {
     13   int success;
     14   mpc_result_t  r;
     15   mpc_parser_t* p = mpc_check(mpc_or(2, mpc_char('a'), mpc_char('b')), free, check_is_a, "Expected 'a'");
     16 
     17   success = mpc_parse("test", "a", p, &r);
     18   PT_ASSERT(success);
     19   PT_ASSERT_STR_EQ(r.output, "a");
     20   if (success) free(r.output); else mpc_err_delete(r.error);
     21 
     22   success = mpc_parse("test", "b", p, &r);
     23   PT_ASSERT(!success);
     24   PT_ASSERT_STR_EQ(r.error->failure, "Expected 'a'");
     25   if (success) free(r.output); else mpc_err_delete(r.error);
     26 
     27   mpc_delete(p);
     28 }
     29 
     30 void test_check_with(void) {
     31   int success;
     32   mpc_result_t  r;
     33   mpc_parser_t* p = mpc_check_with(mpc_or(2, mpc_char('a'), mpc_char('b')), free, check_is, (void*)"a", "Expected 'a'");
     34 
     35   success = mpc_parse("test", "a", p, &r);
     36   PT_ASSERT(success);
     37   if (success) PT_ASSERT_STR_EQ(r.output, "a");
     38   if (success) free(r.output); else mpc_err_delete(r.error);
     39 
     40   success = mpc_parse("test", "b", p, &r);
     41   PT_ASSERT(!success);
     42   if (!success) PT_ASSERT_STR_EQ(r.error->failure, "Expected 'a'");
     43   if (success) free(r.output); else mpc_err_delete(r.error);
     44 
     45   mpc_delete(p);
     46 }
     47 
     48 void test_checkf(void) {
     49   int success;
     50   mpc_result_t  r;
     51   mpc_parser_t* p = mpc_checkf(mpc_or(2, mpc_char('a'), mpc_char('b')), free, check_is_a, "Expected '%s'", "a");
     52 
     53   success = mpc_parse("test", "a", p, &r);
     54   PT_ASSERT(success);
     55   PT_ASSERT_STR_EQ(r.output, "a");
     56   if (success) free(r.output); else mpc_err_delete(r.error);
     57 
     58   success = mpc_parse("test", "b", p, &r);
     59   PT_ASSERT(!success);
     60   PT_ASSERT_STR_EQ(r.error->failure, "Expected 'a'");
     61   if (success) free(r.output); else mpc_err_delete(r.error);
     62 
     63   mpc_delete(p);
     64 }
     65 
     66 void test_check_withf(void) {
     67   int success;
     68   mpc_result_t  r;
     69   mpc_parser_t* p = mpc_check_withf(mpc_or(2, mpc_char('a'), mpc_char('b')), free, check_is, (void*)"a", "Expected '%s'", "a");
     70 
     71   success = mpc_parse("test", "a", p, &r);
     72   PT_ASSERT(success);
     73   if (success) PT_ASSERT_STR_EQ(r.output, "a");
     74   if (success) free(r.output); else mpc_err_delete(r.error);
     75 
     76   success = mpc_parse("test", "b", p, &r);
     77   PT_ASSERT(!success);
     78   if (!success) PT_ASSERT_STR_EQ(r.error->failure, "Expected 'a'");
     79   if (success) free(r.output); else mpc_err_delete(r.error);
     80 
     81   mpc_delete(p);
     82 }
     83 
     84 void suite_combinators(void) {
     85   pt_add_test(test_check,       "Test Check",        "Suite Combinators");
     86   pt_add_test(test_check_with,  "Test Check with",   "Suite Combinators");
     87   pt_add_test(test_checkf,      "Test Check F",      "Suite Combinators");
     88   pt_add_test(test_check_withf, "Test Check with F", "Suite Combinators");
     89 }