eal: switch to architecture specific pause function
[dpdk.git] / test / test / test_common.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <stdio.h>
35 #include <string.h>
36 #include <rte_common.h>
37 #include <rte_hexdump.h>
38 #include <rte_pause.h>
39
40 #include "test.h"
41
42 #define MAX_NUM 1 << 20
43
44 #define FAIL(x)\
45         {printf(x "() test failed!\n");\
46         return -1;}
47
48 /* this is really a sanity check */
49 static int
50 test_macros(int __rte_unused unused_parm)
51 {
52 #define SMALLER 0x1000U
53 #define BIGGER 0x2000U
54 #define PTR_DIFF BIGGER - SMALLER
55 #define FAIL_MACRO(x)\
56         {printf(#x "() test failed!\n");\
57         return -1;}
58
59         uintptr_t unused = 0;
60
61         RTE_SET_USED(unused);
62
63         if ((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF) != BIGGER)
64                 FAIL_MACRO(RTE_PTR_ADD);
65         if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
66                 FAIL_MACRO(RTE_PTR_SUB);
67         if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
68                 FAIL_MACRO(RTE_PTR_DIFF);
69         if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
70                 FAIL_MACRO(RTE_MAX);
71         if (RTE_MIN(SMALLER, BIGGER) != SMALLER)
72                 FAIL_MACRO(RTE_MIN);
73
74         if (strncmp(RTE_STR(test), "test", sizeof("test")))
75                 FAIL_MACRO(RTE_STR);
76
77         return 0;
78 }
79
80 static int
81 test_misc(void)
82 {
83         char memdump[] = "memdump_test";
84         if (rte_bsf32(129))
85                 FAIL("rte_bsf32");
86
87         rte_memdump(stdout, "test", memdump, sizeof(memdump));
88         rte_hexdump(stdout, "test", memdump, sizeof(memdump));
89
90         rte_pause();
91
92         return 0;
93 }
94
95 static int
96 test_align(void)
97 {
98 #define FAIL_ALIGN(x, i, p)\
99         {printf(x "() test failed: %u %u\n", i, p);\
100         return -1;}
101 #define ERROR_FLOOR(res, i, pow) \
102                 (res % pow) ||                                          /* check if not aligned */ \
103                 ((res / pow) != (i / pow))              /* check if correct alignment */
104 #define ERROR_CEIL(res, i, pow) \
105                 (res % pow) ||                                          /* check if not aligned */ \
106                         ((i % pow) == 0 ?                               /* check if ceiling is invoked */ \
107                         val / pow != i / pow :                  /* if aligned */ \
108                         val / pow != (i / pow) + 1)             /* if not aligned, hence +1 */
109
110         uint32_t i, p, val;
111
112         for (i = 1, p = 1; i <= MAX_NUM; i ++) {
113                 if (rte_align32pow2(i) != p)
114                         FAIL_ALIGN("rte_align32pow2", i, p);
115                 if (i == p)
116                         p <<= 1;
117         }
118
119         for (p = 2; p <= MAX_NUM; p <<= 1) {
120
121                 if (!rte_is_power_of_2(p))
122                         FAIL("rte_is_power_of_2");
123
124                 for (i = 1; i <= MAX_NUM; i++) {
125                         /* align floor */
126                         if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
127                                 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
128
129                         val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
130                         if (ERROR_FLOOR(val, i, p))
131                                 FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
132
133                         val = RTE_ALIGN_FLOOR(i, p);
134                         if (ERROR_FLOOR(val, i, p))
135                                 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
136
137                         /* align ceiling */
138                         val = RTE_PTR_ALIGN((uintptr_t) i, p);
139                         if (ERROR_CEIL(val, i, p))
140                                 FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
141
142                         val = RTE_ALIGN(i, p);
143                         if (ERROR_CEIL(val, i, p))
144                                 FAIL_ALIGN("RTE_ALIGN", i, p);
145
146                         val = RTE_ALIGN_CEIL(i, p);
147                         if (ERROR_CEIL(val, i, p))
148                                 FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
149
150                         val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
151                         if (ERROR_CEIL(val, i, p))
152                                 FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
153
154                         /* by this point we know that val is aligned to p */
155                         if (!rte_is_aligned((void*)(uintptr_t) val, p))
156                                 FAIL("rte_is_aligned");
157                 }
158         }
159         return 0;
160 }
161
162 static int
163 test_common(void)
164 {
165         int ret = 0;
166         ret |= test_align();
167         ret |= test_macros(0);
168         ret |= test_misc();
169
170         return ret;
171 }
172
173 REGISTER_TEST_COMMAND(common_autotest, test_common);