Commit | Line | Data |
---|---|---|
c4ccbbf9 MW |
1 | /* -*-c-*- |
2 | * | |
3 | * Example test program | |
4 | * | |
5 | * (c) 2024 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of the mLib utilities library. | |
11 | * | |
12 | * mLib is free software: you can redistribute it and/or modify it under | |
13 | * the terms of the GNU Library General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or (at | |
15 | * your option) any later version. | |
16 | * | |
17 | * mLib is distributed in the hope that it will be useful, but WITHOUT | |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | |
20 | * License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Library General Public | |
23 | * License along with mLib. If not, write to the Free Software | |
24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
25 | * USA. | |
26 | */ | |
27 | ||
28 | /*----- Header files ------------------------------------------------------*/ | |
29 | ||
30 | #include "tvec.h" | |
b1a20bee | 31 | #include "tvec-types.h" |
c4ccbbf9 MW |
32 | |
33 | #include "example.h" | |
34 | ||
35 | /*----- Register allocation -----------------------------------------------*/ | |
36 | ||
37 | enum { | |
38 | /* add */ /* greet */ | |
39 | RZ, RMSG = RZ, | |
40 | RRC, | |
41 | ||
42 | NROUT, | |
43 | ||
44 | RX = NROUT, RNAME = RX, | |
45 | RY, RSZ = RY, | |
46 | ||
47 | NREG | |
48 | }; | |
49 | ||
50 | /*----- Addition test -----------------------------------------------------*/ | |
51 | ||
c4ccbbf9 MW |
52 | static const struct tvec_regdef add_regs[] = { |
53 | { "x", &tvty_int, RX, 0, { &tvrange_int } }, | |
54 | { "y", &tvty_int, RY, 0, { &tvrange_int } }, | |
55 | { "z", &tvty_int, RZ, 0, { &tvrange_int } }, | |
56 | TVEC_ENDREGS | |
57 | }; | |
58 | ||
b1a20bee MW |
59 | void test_add(const struct tvec_reg *in, struct tvec_reg *out, void *ctx) |
60 | { out[RZ].v.i = add(in[RX].v.i, in[RY].v.i); } | |
61 | ||
62 | static const struct tvec_test add_test = | |
63 | { "add", add_regs, 0, test_add }; | |
c4ccbbf9 MW |
64 | |
65 | /*----- Greeting test -----------------------------------------------------*/ | |
66 | ||
67 | void test_greet(const struct tvec_reg *in, struct tvec_reg *out, void *ctx) | |
68 | { | |
69 | tvec_alloctext(&out[RMSG].v, in[RSZ].v.u); | |
70 | out[RRC].v.i = greet(out[RMSG].v.text.p, in[RSZ].v.u, in[RNAME].v.text.p); | |
71 | out[RMSG].v.text.sz = strlen(out[RMSG].v.text.p); | |
72 | } | |
73 | ||
74 | static const struct tvec_regdef greet_regs[] = { | |
75 | { "name", &tvty_text, RNAME, 0 }, | |
76 | { "sz", &tvty_size, RSZ, 0, { &tvrange_size } }, | |
77 | { "msg", &tvty_text, RMSG, TVRF_UNSET }, | |
78 | { "rc", &tvty_int, RRC, 0, { &tvrange_int } }, | |
79 | TVEC_ENDREGS | |
80 | }; | |
81 | ||
b1a20bee MW |
82 | static const struct tvec_test greet_test = |
83 | { "greet", greet_regs, 0, test_greet }; | |
c4ccbbf9 MW |
84 | |
85 | /*----- Main program ------------------------------------------------------*/ | |
86 | ||
b1a20bee MW |
87 | static const struct tvec_test *const tests[] = { |
88 | &add_test, | |
89 | &greet_test, | |
90 | 0 | |
c4ccbbf9 MW |
91 | }; |
92 | ||
93 | static const struct tvec_config test_config = | |
94 | { tests, NROUT, NREG, sizeof(struct tvec_reg) }; | |
95 | ||
96 | int main(int argc, char *argv[]) | |
97 | { return (tvec_main(argc, argv, &test_config, 0)); } | |
98 | ||
99 | /*----- That's all, folks -------------------------------------------------*/ |