/ src / core / object-and-operation.c
object-and-operation.c
 1  // SPDX-FileCopyrightText: 2023-2025 Le'Sec Core collective
 2  //
 3  // SPDX-License-Identifier: LGPL-3.0-or-later
 4  
 5  // Objects and operations are pure factories, there is no library code to
 6  // implement for them.  Therefore, this is purely a unit test, and because
 7  // the example object (the "roller") has a constructor (a generator), it
 8  // ends up being a small test of an operation as well.
 9  
10  #include <stdlib.h>
11  #include <stdarg.h>
12  #include <string.h>
13  #include <lscore/object.h>
14  
15  #ifdef TEST_OBJECT
16  
17  // Implement the roller "roller1"
18  #include "roller1.inc"
19  
20  int main(int argc, char *argv[])
21  {
22    TEST_roller_t *r = NULL;
23    TEST_roller_generator_t *gen = NULL;
24    TEST_roller_extractor_t *ext = NULL;
25    TEST_roller_runner_t *run = NULL;
26    LE_STATUS sts;
27    int e = 0;
28    const unsigned int seed                       = 1;
29    // We know that the seed become 56473, 48019, 33850 and 48391
30    // => inner_roll_size = (56473 % (5 - 2 + 1)) + 2 = 3         [0]
31    // => inner_roll_num  = (48019 % (3 - 1 + 1)) + 1 = 2         [1]
32    // => outer_roll_size = (33850 % (5 - 2 + 1)) + 2 = 4         [2]
33    // => outer_roll_num  = (48391 % (3 - 1 + 1)) + 1 = 2         [3]
34    const unsigned int expected_inner_roll_size   = 3;
35    const unsigned int expected_inner_roll_num    = 2;
36    const unsigned int expected_outer_roll_size   = 4;
37    const unsigned int expected_outer_roll_num    = 2;
38    unsigned int outnum;
39    const char input[]                            = "Foxies Foxes";
40    const char expected_output[]                  = "Fo esxoxFesi";
41    char output[sizeof(input)];
42  
43    struct test_roller1_data_t *check;
44  
45    // Setup
46    if (!LE_status_is_OK(sts = TEST_new_roller1(&r))
47        || !LE_status_is_OK(sts = TEST_get_roller_generator(r, &gen))
48        || !LE_status_is_OK(sts = TEST_set_roller_generation_param
49                            (gen, 1, (LSC_param_t[]){
50                              { (void *)&seed, sizeof(seed), NULL },
51                              { NULL, 0, NULL }
52                            }))
53        || !LE_status_is_OK(sts = TEST_generate_roller(gen))
54        || !LE_status_is_OK(sts = TEST_get_roller_extractor(r, &ext))
55        || !LE_status_is_OK(sts = TEST_get_roller_extraction_param
56                            (ext, 0, (LSC_param_t[]){
57                              { &outnum, sizeof(outnum), NULL },
58                              { NULL, 0, NULL }
59                            }))
60        || outnum != expected_inner_roll_size
61        || !LE_status_is_OK(sts = TEST_get_roller_extraction_param
62                            (ext, 1, (LSC_param_t[]){
63                              { &outnum, sizeof(outnum), NULL },
64                              { NULL, 0, NULL }
65                            }))
66        || outnum != expected_inner_roll_num
67        || !LE_status_is_OK(sts = TEST_get_roller_extraction_param
68                            (ext, 2, (LSC_param_t[]){
69                              { &outnum, sizeof(outnum), NULL },
70                              { NULL, 0, NULL }
71                            }))
72        || outnum != expected_outer_roll_size
73        || !LE_status_is_OK(sts = TEST_get_roller_extraction_param
74                            (ext, 3, (LSC_param_t[]){
75                              { &outnum, sizeof(outnum), NULL },
76                              { NULL, 0, NULL }
77                            }))
78        || outnum != expected_outer_roll_num)
79      e++;
80  
81    // Run the roller directly
82    if (!LE_status_is_OK(sts = TEST_roll(r, input, output, sizeof(output)))
83        || strcmp(output, expected_output) != 0)
84      e++;
85  
86    // Run the roller through the runner primitive
87    if (!LE_status_is_OK(sts = TEST_get_roller_runner(r, &run))
88        || !LE_status_is_OK(sts = TEST_run_roll(run, input, output, sizeof(output)))
89        || strcmp(output, expected_output) != 0)
90      e++;
91  
92    // Cleanup and check.  We still have pointers to that bag
93    if (!LE_status_is_OK(sts = TEST_free_roller(r)))
94      e++;
95  
96    exit(e);
97  }
98  #endif