Alexandre Lision | 7c6f4a6 | 2013-09-05 13:27:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com> |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or modify |
| 5 | ** it under the terms of the GNU Lesser General Public License as published by |
| 6 | ** the Free Software Foundation; either version 2.1 of the License, or |
| 7 | ** (at your option) any later version. |
| 8 | ** |
| 9 | ** This program is distributed in the hope that it will be useful, |
| 10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | ** GNU Lesser General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU Lesser General Public License |
| 15 | ** along with this program; if not, write to the Free Software |
| 16 | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "sfconfig.h" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <errno.h> |
| 25 | #include <inttypes.h> |
| 26 | |
| 27 | #if HAVE_UNISTD_H |
| 28 | #include <unistd.h> |
| 29 | #endif |
| 30 | |
| 31 | #include "common.h" |
| 32 | #include "sfendian.h" |
| 33 | |
| 34 | #include "test_main.h" |
| 35 | |
| 36 | #define FMT_SHORT "0x%04x\n" |
| 37 | #define FMT_INT "0x%08x\n" |
| 38 | #define FMT_INT64 "0x%016" PRIx64 "\n" |
| 39 | |
| 40 | /*============================================================================== |
| 41 | ** Test functions. |
| 42 | */ |
| 43 | |
| 44 | |
| 45 | static void |
| 46 | dump_short_array (const char * name, short * data, int datalen) |
| 47 | { int k ; |
| 48 | |
| 49 | printf ("%-6s : ", name) ; |
| 50 | for (k = 0 ; k < datalen ; k++) |
| 51 | printf (FMT_SHORT, data [k]) ; |
| 52 | putchar ('\n') ; |
| 53 | } /* dump_short_array */ |
| 54 | |
| 55 | static void |
| 56 | test_endswap_short (void) |
| 57 | { short orig [4], first [4], second [4] ; |
| 58 | int k ; |
| 59 | |
| 60 | printf (" %-40s : ", "test_endswap_short") ; |
| 61 | fflush (stdout) ; |
| 62 | |
| 63 | for (k = 0 ; k < ARRAY_LEN (orig) ; k++) |
| 64 | orig [k] = 0x3210 + k ; |
| 65 | |
| 66 | endswap_short_copy (first, orig, ARRAY_LEN (first)) ; |
| 67 | endswap_short_copy (second, first, ARRAY_LEN (second)) ; |
| 68 | |
| 69 | if (memcmp (orig, first, sizeof (orig)) == 0) |
| 70 | { printf ("\n\nLine %d : test 1 : these two array should not be the same:\n\n", __LINE__) ; |
| 71 | dump_short_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 72 | dump_short_array ("first", first, ARRAY_LEN (first)) ; |
| 73 | exit (1) ; |
| 74 | } ; |
| 75 | |
| 76 | if (memcmp (orig, second, sizeof (orig)) != 0) |
| 77 | { printf ("\n\nLine %d : test 2 : these two array should be the same:\n\n", __LINE__) ; |
| 78 | dump_short_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 79 | dump_short_array ("second", second, ARRAY_LEN (second)) ; |
| 80 | exit (1) ; |
| 81 | } ; |
| 82 | |
| 83 | endswap_short_array (first, ARRAY_LEN (first)) ; |
| 84 | |
| 85 | if (memcmp (orig, first, sizeof (orig)) != 0) |
| 86 | { printf ("\n\nLine %d : test 3 : these two array should be the same:\n\n", __LINE__) ; |
| 87 | dump_short_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 88 | dump_short_array ("first", first, ARRAY_LEN (first)) ; |
| 89 | exit (1) ; |
| 90 | } ; |
| 91 | |
| 92 | endswap_short_copy (first, orig, ARRAY_LEN (first)) ; |
| 93 | endswap_short_copy (first, first, ARRAY_LEN (first)) ; |
| 94 | |
| 95 | if (memcmp (orig, first, sizeof (orig)) != 0) |
| 96 | { printf ("\n\nLine %d : test 4 : these two array should be the same:\n\n", __LINE__) ; |
| 97 | dump_short_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 98 | dump_short_array ("first", first, ARRAY_LEN (first)) ; |
| 99 | exit (1) ; |
| 100 | } ; |
| 101 | |
| 102 | puts ("ok") ; |
| 103 | } /* test_endswap_short */ |
| 104 | |
| 105 | static void |
| 106 | dump_int_array (const char * name, int * data, int datalen) |
| 107 | { int k ; |
| 108 | |
| 109 | printf ("%-6s : ", name) ; |
| 110 | for (k = 0 ; k < datalen ; k++) |
| 111 | printf (FMT_INT, data [k]) ; |
| 112 | putchar ('\n') ; |
| 113 | } /* dump_int_array */ |
| 114 | |
| 115 | static void |
| 116 | test_endswap_int (void) |
| 117 | { int orig [4], first [4], second [4] ; |
| 118 | int k ; |
| 119 | |
| 120 | printf (" %-40s : ", "test_endswap_int") ; |
| 121 | fflush (stdout) ; |
| 122 | |
| 123 | for (k = 0 ; k < ARRAY_LEN (orig) ; k++) |
| 124 | orig [k] = 0x76543210 + k ; |
| 125 | |
| 126 | endswap_int_copy (first, orig, ARRAY_LEN (first)) ; |
| 127 | endswap_int_copy (second, first, ARRAY_LEN (second)) ; |
| 128 | |
| 129 | if (memcmp (orig, first, sizeof (orig)) == 0) |
| 130 | { printf ("\n\nLine %d : test 1 : these two array should not be the same:\n\n", __LINE__) ; |
| 131 | dump_int_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 132 | dump_int_array ("first", first, ARRAY_LEN (first)) ; |
| 133 | exit (1) ; |
| 134 | } ; |
| 135 | |
| 136 | if (memcmp (orig, second, sizeof (orig)) != 0) |
| 137 | { printf ("\n\nLine %d : test 2 : these two array should be the same:\n\n", __LINE__) ; |
| 138 | dump_int_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 139 | dump_int_array ("second", second, ARRAY_LEN (second)) ; |
| 140 | exit (1) ; |
| 141 | } ; |
| 142 | |
| 143 | endswap_int_array (first, ARRAY_LEN (first)) ; |
| 144 | |
| 145 | if (memcmp (orig, first, sizeof (orig)) != 0) |
| 146 | { printf ("\n\nLine %d : test 3 : these two array should be the same:\n\n", __LINE__) ; |
| 147 | dump_int_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 148 | dump_int_array ("first", first, ARRAY_LEN (first)) ; |
| 149 | exit (1) ; |
| 150 | } ; |
| 151 | |
| 152 | endswap_int_copy (first, orig, ARRAY_LEN (first)) ; |
| 153 | endswap_int_copy (first, first, ARRAY_LEN (first)) ; |
| 154 | |
| 155 | if (memcmp (orig, first, sizeof (orig)) != 0) |
| 156 | { printf ("\n\nLine %d : test 4 : these two array should be the same:\n\n", __LINE__) ; |
| 157 | dump_int_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 158 | dump_int_array ("first", first, ARRAY_LEN (first)) ; |
| 159 | exit (1) ; |
| 160 | } ; |
| 161 | |
| 162 | puts ("ok") ; |
| 163 | } /* test_endswap_int */ |
| 164 | |
| 165 | static void |
| 166 | dump_int64_t_array (const char * name, int64_t * data, int datalen) |
| 167 | { int k ; |
| 168 | |
| 169 | printf ("%-6s : ", name) ; |
| 170 | for (k = 0 ; k < datalen ; k++) |
| 171 | printf (FMT_INT64, data [k]) ; |
| 172 | putchar ('\n') ; |
| 173 | } /* dump_int64_t_array */ |
| 174 | |
| 175 | static void |
| 176 | test_endswap_int64_t (void) |
| 177 | { int64_t orig [4], first [4], second [4] ; |
| 178 | int k ; |
| 179 | |
| 180 | printf (" %-40s : ", "test_endswap_int64_t") ; |
| 181 | fflush (stdout) ; |
| 182 | |
| 183 | for (k = 0 ; k < ARRAY_LEN (orig) ; k++) |
| 184 | orig [k] = 0x0807050540302010LL + k ; |
| 185 | |
| 186 | endswap_int64_t_copy (first, orig, ARRAY_LEN (first)) ; |
| 187 | endswap_int64_t_copy (second, first, ARRAY_LEN (second)) ; |
| 188 | |
| 189 | if (memcmp (orig, first, sizeof (orig)) == 0) |
| 190 | { printf ("\n\nLine %d : test 1 : these two array should not be the same:\n\n", __LINE__) ; |
| 191 | dump_int64_t_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 192 | dump_int64_t_array ("first", first, ARRAY_LEN (first)) ; |
| 193 | exit (1) ; |
| 194 | } ; |
| 195 | |
| 196 | if (memcmp (orig, second, sizeof (orig)) != 0) |
| 197 | { printf ("\n\nLine %d : test 2 : these two array should be the same:\n\n", __LINE__) ; |
| 198 | dump_int64_t_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 199 | dump_int64_t_array ("second", second, ARRAY_LEN (second)) ; |
| 200 | exit (1) ; |
| 201 | } ; |
| 202 | |
| 203 | endswap_int64_t_array (first, ARRAY_LEN (first)) ; |
| 204 | |
| 205 | if (memcmp (orig, first, sizeof (orig)) != 0) |
| 206 | { printf ("\n\nLine %d : test 3 : these two array should be the same:\n\n", __LINE__) ; |
| 207 | dump_int64_t_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 208 | dump_int64_t_array ("first", first, ARRAY_LEN (first)) ; |
| 209 | exit (1) ; |
| 210 | } ; |
| 211 | |
| 212 | endswap_int64_t_copy (first, orig, ARRAY_LEN (first)) ; |
| 213 | endswap_int64_t_copy (first, first, ARRAY_LEN (first)) ; |
| 214 | |
| 215 | if (memcmp (orig, first, sizeof (orig)) != 0) |
| 216 | { printf ("\n\nLine %d : test 4 : these two array should be the same:\n\n", __LINE__) ; |
| 217 | dump_int64_t_array ("orig", orig, ARRAY_LEN (orig)) ; |
| 218 | dump_int64_t_array ("first", first, ARRAY_LEN (first)) ; |
| 219 | exit (1) ; |
| 220 | } ; |
| 221 | |
| 222 | puts ("ok") ; |
| 223 | } /* test_endswap_int64_t */ |
| 224 | |
| 225 | |
| 226 | |
| 227 | void |
| 228 | test_endswap (void) |
| 229 | { |
| 230 | test_endswap_short () ; |
| 231 | test_endswap_int () ; |
| 232 | test_endswap_int64_t () ; |
| 233 | |
| 234 | } /* test_endswap */ |
| 235 | |