core: Rename GP_Context.c -> GP_Pixmap.c
[gfxprim.git] / tests / core / Convert_Scale.gen.c.t
blob85f86b3a5867d82d9f30989b371ede64b3a44732
1 @ include source.t
2 /*
3  * ConvertScale tests.
4  *
5  * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz>
6  */
8 #include <stdio.h>
9 #include <math.h>
10 #include <stdint.h>
12 #include <core/GP_Convert_Scale.gen.h>
14 #include "tst_test.h"
16 @ max_in = 24
17 @ max_out = 16
19 @ for i in range(1, max_in):
20 @     for j in range(1, max_out):
21 static int check_convert_{{ i }}_{{ j }}(void)
23         unsigned int v, fail = 0;
24         uint32_t res, exp_res;
25         float fres;
27         for (v = 0; v < {{ 2 ** i - 1 }}; v++) {
28                 res = GP_SCALE_VAL_{{ i }}_{{ j }}(v);
29 @         if j > i:
30                 /*
31                  * We have {{ 2**i }} values and we need to map them to
32                  * subset of {{ 2**j }} values while making sure 0 -> 0
33                  * and {{ 2**i - 1 }} -> {{ 2**j - 1 }} and that the
34                  * mapping is as evenly distributed as possible.
35                  *
36                  * So we map the input to 0-1 interval by dividing it by
37                  * maximal input value {{ 2**i - 1 }} and then multiply
38                  * it by output maximal value {{ 2**j - 1}}.
39                  */
40                 fres = (v / {{ (2.00 ** i - 1) }}) * {{ (2.00 ** j - 1) }};
41                 exp_res = round(fres);
42 @         else:
43                 /*
44                  * We have {{ 2**i }} values that must be mapped to {{ 2**j }}
45                  * so we do simple division and floor() which maps the values
46                  * evenly, 0 -> 0 and {{ 2**i - 1 }} -> {{ 2**j - 1 }}.
47                  *
48                  * In terms for implementation this is just bitshift.
49                  */
50                 fres = v * {{ (2.00 ** j) / (2.00 ** i) }};
51                 exp_res = floor(fres);
52 @         end
54                 if (res != exp_res) {
55                         if (fail < 5)
56                                 tst_msg("GP_SCALE_{{ i }}_{{ j }}(%i) = %i, "
57                                         "expected %i %f", v, res, exp_res, fres);
58                         fail++;
59                 }
60         }
62         if (fail) {
63                 if (fail > 5)
64                         tst_msg("+ next %u failures", fail - 5);
65                 return TST_FAILED;
66         }
68         return TST_SUCCESS;
71 @ endfor
73 const struct tst_suite tst_suite = {
74         .suite_name = "Convert Scale Testsuite",
75         .tests = {
76 @ for i in range(1, max_in):
77 @     for j in range(1, max_out):
78                 {.name = "SCALE_{{ i }}_{{ j }}()",
79                  .tst_fn = check_convert_{{ i }}_{{ j }}},
80 @ end
81                 {.name = NULL}
82         }