File: dispatch-t.c

package info (click to toggle)
inn2 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,720 kB
  • ctags: 8,983
  • sloc: ansic: 92,499; sh: 13,509; perl: 12,921; makefile: 2,985; yacc: 842; python: 342; lex: 255
file content (141 lines) | stat: -rw-r--r-- 4,361 bytes parent folder | download | duplicates (3)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* $Id: dispatch-t.c 6838 2004-05-19 06:11:42Z rra $ */
/* dispatch test suite. */

#include "config.h"
#include "clibrary.h"

#include "inn/dispatch.h"
#include "inn/vector.h"
#include "libtest.h"

/* Prototypes for command callbacks. */
static void command_0(struct cvector *, void *);
static void command_1(struct cvector *, void *);
static void command_4(struct cvector *, void *);

/* Test command dispatch table. */
const struct dispatch commands[] = {
    { "0", command_0, 0, 0, NULL },
    { "1", command_1, 1, 1, NULL },
    { "4", command_4, 2, 4, NULL }
};

/* Global string indicating the last command handler that was called. */
static const char *last_handler = NULL;

/* Global flag indicating whether the last handler was okay. */
static bool last_args_valid = false;

/* Command that takes 0 arguments. */
static void
command_0(struct cvector *command, void *cookie)
{
    last_handler = "0";
    last_args_valid = true;
    if (command->count != 1 || strcmp(command->strings[0], "0") != 0)
        last_args_valid = false;
    if (cookie != &last_handler)
        last_args_valid = false;
}

/* Command that takes 1 argument. */
static void
command_1(struct cvector *command, void *cookie)
{
    last_handler = "1";
    last_args_valid = true;
    if (command->count != 2 || strcmp(command->strings[0], "1") != 0)
        last_args_valid = false;
    if (cookie != &last_handler)
        last_args_valid = false;
}

/* Command that takes 2-4 arguments. */
static void
command_4(struct cvector *command, void *cookie)
{
    last_handler = "4";
    last_args_valid = true;
    if (command->count < 3 || command->count > 5)
        last_args_valid = false;
    if (strcmp(command->strings[0], "4") != 0)
        last_args_valid = false;
    if (cookie != &last_handler)
        last_args_valid = false;
}

/* Handler for unknown commands. */
static void
command_unknown(struct cvector *command UNUSED, void *cookie)
{
    last_handler = "unknown";
    last_args_valid = (cookie == &last_handler);
}

/* Handler for syntax errors. */
static void
command_syntax(struct cvector *command UNUSED, void *cookie)
{
    last_handler = "syntax";
    last_args_valid = (cookie == &last_handler);
}

int
main(void)
{
    struct cvector *command;

    test_init(18);

    command = cvector_new();
    cvector_resize(command, 6);
    last_handler = NULL;
    cvector_add(command, "0");
    dispatch(command, commands, ARRAY_SIZE(commands), command_unknown,
             command_syntax, &last_handler);
    ok_string(1, "0", last_handler);
    ok(2, last_args_valid);
    command->strings[0] = "1";
    cvector_add(command, "arg1");
    dispatch(command, commands, ARRAY_SIZE(commands), command_unknown,
             command_syntax, &last_handler);
    ok_string(3, "1", last_handler);
    ok(4, last_args_valid);
    command->strings[0] = "4";
    dispatch(command, commands, ARRAY_SIZE(commands), command_unknown,
             command_syntax, &last_handler);
    ok_string(5, "syntax", last_handler);
    ok(6, last_args_valid);
    cvector_add(command, "arg2");
    dispatch(command, commands, ARRAY_SIZE(commands), command_unknown,
             command_syntax, &last_handler);
    ok_string(7, "4", last_handler);
    ok(8, last_args_valid);
    cvector_add(command, "arg3");
    dispatch(command, commands, ARRAY_SIZE(commands), command_unknown,
             command_syntax, &last_handler);
    ok_string(9, "4", last_handler);
    ok(10, last_args_valid);
    cvector_add(command, "arg4");
    dispatch(command, commands, ARRAY_SIZE(commands), command_unknown,
             command_syntax, &last_handler);
    ok_string(11, "4", last_handler);
    ok(12, last_args_valid);
    cvector_add(command, "arg5");
    dispatch(command, commands, ARRAY_SIZE(commands), command_unknown,
             command_syntax, &last_handler);
    ok_string(13, "syntax", last_handler);
    ok(14, last_args_valid);
    command->strings[0] = "5";
    dispatch(command, commands, ARRAY_SIZE(commands), command_unknown,
             command_syntax, &last_handler);
    ok_string(15, "unknown", last_handler);
    ok(16, last_args_valid);
    cvector_resize(command, 0);
    dispatch(command, commands, ARRAY_SIZE(commands), command_unknown,
             command_syntax, &last_handler);
    ok_string(17, "unknown", last_handler);
    ok(18, last_args_valid);

    return 0;
}