a5fab84c24edda2ab1efdc90ce6e0cb8f2bed593
[dpdk.git] / app / test / test_common.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <rte_common.h>
38 #include <rte_hexdump.h>
39
40 #include <cmdline_parse.h>
41
42 #include "test.h"
43
44 #define MAX_NUM 1 << 20
45
46 #define FAIL(x)\
47         {printf(x "() test failed!\n");\
48         return -1;}
49
50 /* this is really a sanity check */
51 static int
52 test_macros(int __rte_unused unused_parm)
53 {
54 #define SMALLER 0x1000U
55 #define BIGGER 0x2000U
56 #define PTR_DIFF BIGGER - SMALLER
57 #define FAIL_MACRO(x)\
58         {printf(#x "() test failed!\n");\
59         return -1;}
60
61         uintptr_t unused = 0;
62
63         RTE_SET_USED(unused);
64
65         if (RTE_PTR_ADD(SMALLER, PTR_DIFF) != BIGGER)
66                 FAIL_MACRO(RTE_PTR_ADD);
67         if (RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
68                 FAIL_MACRO(RTE_PTR_SUB);
69         if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
70                 FAIL_MACRO(RTE_PTR_DIFF);
71         if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
72                 FAIL_MACRO(RTE_MAX);
73         if (RTE_MIN(SMALLER, BIGGER) != SMALLER)
74                 FAIL_MACRO(RTE_MIN);
75
76         if (strncmp(RTE_STR(test), "test", sizeof("test")))
77                 FAIL_MACRO(RTE_STR);
78
79         return 0;
80 }
81
82 static int
83 test_misc(void)
84 {
85         char memdump[] = "memdump_test";
86         if (rte_bsf32(129))
87                 FAIL("rte_bsf32");
88
89         rte_memdump("test", memdump, sizeof(memdump));
90         rte_hexdump("test", memdump, sizeof(memdump));
91
92         rte_pause();
93
94         return 0;
95 }
96
97 static int
98 test_align(void)
99 {
100 #define FAIL_ALIGN(x, i, p)\
101         {printf(x "() test failed: %u %u\n", i, p);\
102         return -1;}
103 #define ERROR_FLOOR(res, i, pow) \
104                 (res % pow) ||                                          /* check if not aligned */ \
105                 ((res / pow) != (i / pow))              /* check if correct alignment */
106 #define ERROR_CEIL(res, i, pow) \
107                 (res % pow) ||                                          /* check if not aligned */ \
108                         ((i % pow) == 0 ?                               /* check if ceiling is invoked */ \
109                         val / pow != i / pow :                  /* if aligned */ \
110                         val / pow != (i / pow) + 1)             /* if not aligned, hence +1 */
111
112         uint32_t i, p, val;
113
114         for (i = 1, p = 1; i <= MAX_NUM; i ++) {
115                 if (rte_align32pow2(i) != p)
116                         FAIL_ALIGN("rte_align32pow2", i, p);
117                 if (i == p)
118                         p <<= 1;
119         }
120
121         for (p = 2; p <= MAX_NUM; p <<= 1) {
122
123                 if (!rte_is_power_of_2(p))
124                         FAIL("rte_is_power_of_2");
125
126                 for (i = 1; i <= MAX_NUM; i++) {
127                         /* align floor */
128                         if (rte_align_floor_int((uintptr_t)i, p) % p)
129                                 FAIL_ALIGN("rte_align_floor_int", i, p);
130
131                         val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
132                         if (ERROR_FLOOR(val, i, p))
133                                 FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
134
135                         val = RTE_ALIGN_FLOOR(i, p);
136                         if (ERROR_FLOOR(val, i, p))
137                                 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
138
139                         /* align ceiling */
140                         val = RTE_PTR_ALIGN((uintptr_t) i, p);
141                         if (ERROR_CEIL(val, i, p))
142                                 FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
143
144                         val = RTE_ALIGN(i, p);
145                         if (ERROR_CEIL(val, i, p))
146                                 FAIL_ALIGN("RTE_ALIGN", i, p);
147
148                         val = RTE_ALIGN_CEIL(i, p);
149                         if (ERROR_CEIL(val, i, p))
150                                 FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
151
152                         val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
153                         if (ERROR_CEIL(val, i, p))
154                                 FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
155
156                         /* by this point we know that val is aligned to p */
157                         if (!rte_is_aligned((void*)(uintptr_t) val, p))
158                                 FAIL("rte_is_aligned");
159                 }
160         }
161         return 0;
162 }
163
164 int
165 test_common(void)
166 {
167         int ret = 0;
168         ret |= test_align();
169         ret |= test_macros(0);
170         ret |= test_misc();
171
172         return ret;
173 }