-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestxs.c
More file actions
107 lines (80 loc) · 2.14 KB
/
testxs.c
File metadata and controls
107 lines (80 loc) · 2.14 KB
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
/* xstring correctness test
* by Chip Salzenberg <chip@pobox.com> */
#include "xstring.h"
#include <lauxlib.h>
#include <lualib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
/* this bit is Unix-specific */
#include <unistd.h> /* getopt, write */
static const char
wool[] = "Pull the wool over your own eyes!";
static void die(const char *fmt, ...);
static void register_funcs(lua_State *L);
int main(int argc, char **argv) {
lua_State *L;
xsbuf_t *buf;
/*
* set up Lua environ
*/
L = luaL_newstate();
assert(L);
luaL_openlibs(L);
/*
* load test harness; this also loads the xstring module;
* and register the test-specific functions
*/
if (luaL_dofile(L, "testxs.lua"))
die("Can't load testxs.lua: %s\n", lua_tostring(L, -1));
lua_settop(L, 0);
register_funcs(L);
/*
* call test entry point with a few preconstructed xstrings;
* if it fails, we do too
*/
lua_getglobal(L, "testxs");
buf = xsbuf_new(wool, strlen(wool));
xstring_new(L, buf);
xsbuf_discard(buf); /* this string is always valid */
if (lua_pcall(L, 1, 0, 0))
die("self-test failed: %s\n", lua_tostring(L, -1));
printf("self-test OK\n");
/*
* outahere
*/
lua_close(L);
return 0;
}
static void die(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(1);
}
/*
* example xstring test functions
*/
static int testxs_xfindbyte(lua_State *L) {
size_t haystack_len, needle_len;
const char *haystack = xstring_check( L, 1, &haystack_len);
const char *needle = luaL_checklstring(L, 2, &needle_len );
const char *p;
if (needle_len == 1 && (p = memchr(haystack, needle[0], haystack_len)))
lua_pushinteger(L, (p - haystack) + 1);
else
lua_pushnil(L);
return 1;
}
static const luaL_Reg testxs_regf[] = {
{ "xfindbyte", testxs_xfindbyte },
{ 0, 0 }
};
static void register_funcs(lua_State *L) {
lua_pushvalue(L, LUA_GLOBALSINDEX);
luaL_register(L, NULL, testxs_regf);
lua_pop(L, 1);
}