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