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
|
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "honeysuckle.h"
/* minunit testing macros modified from those at
www.jera.com/techinfo/jtns/jtn002.html */
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
#define mu_run_test(name, test) do { \
lua_State *L = luaL_newstate(); \
luaL_openlibs(L); \
char *message = test(L); \
lua_close(L); \
tests_run++; \
if (message) { \
printf("test '%s' failed: %s\n", name, message); \
tests_failed++; \
} \
} while (0)
int tests_run = 0;
int tests_failed = 0;
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* tests for hs_pushstring
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
static char* push_noformat(lua_State *L)
{
hs_pushstring(L, "a string");
mu_assert("no string at top of stack!", lua_isstring(L, -1));
const char *string = lua_tostring(L, -1);
mu_assert("string != 'a string'", strcmp(string, "a string") == 0);
return 0;
}
static char* push_formatint(lua_State *L)
{
hs_pushstring(L, "%d is 5", 5);
mu_assert("no string at top of stack!", lua_isstring(L, -1));
const char *string = lua_tostring(L, -1);
mu_assert("string != '5 is 5'", strcmp(string, "5 is 5") == 0);
return 0;
}
static char* push_formatstring(lua_State *L)
{
hs_pushstring(L, "%s is 'hello'", "hello");
mu_assert("no string at top of stack!", lua_isstring(L, -1));
const char *string = lua_tostring(L, -1);
mu_assert("string != 'hello is 'hello''",
strcmp(string, "hello is 'hello'") == 0);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* RUN TESTS
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int main()
{
printf("================ start tests ================\n\n");
printf("running hs_pushstring() tests...\n");
mu_run_test("hs_pushstring (no printf formatting)", push_noformat);
mu_run_test("hs_pushstring (integer formatting)", push_formatint);
mu_run_test("hs_pushstring (string formatting)", push_formatstring);
printf("\n=============== tests finished ===============\n\n");
printf("ran %d tests, %d failed\n", tests_run, tests_failed);
return 0;
}
|