summaryrefslogtreecommitdiff
path: root/tests/test.c
blob: 85c1ad76ba373eecfe8613124a5bd3c2c33af55e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

#include "lily-test.h"

LILY_INIT();

void run_test(const char *name, lily_test (*fp)())
{
   printf("%s... ", name);
   const char *result = fp();
   if (result != 0) {
      printf("FAILED (%s)\n", result);
   }
   else
      printf("OK\n");
}


lily_test check_init()
{
   // should fail to compile if lily_test_data is undefined
   if (lily_test_data.tests_run != 0)
      return "tests_run is not equal to zero!";
   if (lily_test_data.tests_failed != 0)
      return "tests_failed is not equal to zero!";
   return 0;
}


int get_message(char **destination, const char *source)
{
   const char *s = source;
   size_t size = 0;
   while (*s != '\n') {
      if (*s == 0)
	 return false;
      s++;
      size++;
   }

   *destination = malloc((size+1) * sizeof(char));
   strncpy(*destination, source, size);
   (*destination)[size] = 0;
   return true;
}

#define assert_msg "message"

lily_test wrap_assert(bool statement)
{
   lily_assert_msg(statement, assert_msg);
   return 0;
}

lily_test check_assert()
{
   if (wrap_assert(true) != 0)
      return "true assertion did not return 0!";

   const char *result = wrap_assert(false);
   
   if (result == 0)
      return "false assertion returned zero!";

   char *message;
   if (!get_message(&message, result))
      return "false assertion contained malformed message!";

   if (strcmp(message, assert_msg) != 0)
      return "false assertion message was not '" assert_msg "'!";
   free(message);

   return 0;
}


const char *assert_eq_true()
{
   lily_assert_equal(15, 15);
   return 0;
}
const char *assert_eq_false()
{
   lily_assert_equal(14, 15);
   return 0;
}

lily_test check_other_asserts()
{
   if (assert_eq_true() != 0)
      return "equality assertion returned non-zero when true!";
   const char *result = assert_eq_false();
   if (result == 0)
      return "equality assertion returned zero when false!";
   char *message;
   if (!get_message(&message, result))
      return "false equality assertion returned malformed message!";
   if (strcmp(message, "'14' is not equal to '15'") != 0)
      return "false equality assertion returned incorrect message!";

   return 0;
}


int main()
{
   run_test("check LILY_INIT()", check_init);
   run_test("check basic assertion", check_assert);
   run_test("check other asserts", check_other_asserts);

   printf("all tests finished.\n");
   return 0;
}