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