bytes_by_prefix_test.c revision fed9641096e27f79a0f2d9adfe9839dd8d11dc0f
1/* 2 * Copyright (C) 2012 Marios Makris <marios.makris@gmail.com> 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it is 13 * free of the rightful claim of any third person regarding infringement 14 * or the like. Any license provided herein, whether implied or 15 * otherwise, applies only to this software file. Patent licenses, if 16 * any, provided herein do not apply to combinations of this program with 17 * other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, write the Free Software Foundation, Inc., 21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 22 */ 23 24/* 25 * Test program for the bytes_by_prefix program in /lib 26 * 27 * This program tests a few predefined values against the expected predefined 28 * results, upon sucesfull completion, it prints the message: 29 * "Tests sucesfully completed!" else it prints that there were an error 30 * and the value as well as the type on which it failed (int, long, long long) 31 * at the time of the error in order for someone to be able to trace 32 * it back as well as the total number of errors encountered along with the 33 * message: "Some test(s): (number of tests) failed please review!" 34 */ 35 36#include <stdio.h> 37#include <stdlib.h> 38 39#include "bytes_by_prefix.h" 40 41struct test_vals { 42 char *val; 43 long long res; 44}; 45 46/* 47 * Array with generic values suitable for all operations. 48 */ 49struct test_vals test[] = { 50 {"1", 1}, 51 {"5", 5}, 52 {"10", 10}, 53 {"552558", 552558}, 54 {"0", 0}, 55 {"1b", 512}, 56 {"5b", 2560}, 57 {"0b", 0}, 58 {"1k", 1024}, 59 {"5k", 5120}, 60 {"552558k", 565819392}, 61 {"0k", 0}, 62 {"1m", 1048576}, 63 {"5m", 5242880}, 64 {"0m", 0}, 65 {"1g", 1073741824}, 66 {"0g", 0}, 67 /* 68 * Negative Test Values 69 */ 70 {"a", -1}, 71 {"k", -1}, 72 {"m", -1}, 73 {"g", -1}, 74 {"K", -1}, 75 {"M", -1}, 76 {"G", -1}, 77 {"5km", -1}, 78 {"1a", -1}, 79 {"1mabc", -1}, 80 {"a1", -1}, 81 {"k1", -1}, 82 {"1 k", -1}, 83 {"1k g", -1}, 84 {"-5", -1}, 85 {"-5b", -1}, 86 {"-2k", -1}, 87 {"-2m", -1}, 88 {"-2g", -1}, 89 {"-2K", -1}, 90 {"-2M", -1}, 91 {"-2G", -1} 92}; 93 94/* 95 * Specific values for int operations 96 */ 97struct test_vals test_i[] = { 98/* 99 * In case of 64b system as the results of capital multipliers are multiplied 100 * by the sizeof(long) or sizeof(long long) respectively. 101 * Check "/lib/bytes_by_prefix.c" file for more information. 102 */ 103#if __SIZEOF_LONG__ == 8 104 {"5K", 40960}, 105 {"0K", 0}, 106 {"5M", 41943040} 107/* 108 * In case of 32b system as the results of capital multipliers are multiplied 109 * by the sizeof(long) or sizeof(long long) respectively. 110 * Check "/lib/bytes_by_prefix.c" file for more information. 111 */ 112#else 113 {"5K", 20480}, 114 {"0K", 0}, 115 {"5M", 20971520} 116#endif 117}; 118 119/* 120 * Specific values for long operations 121 */ 122struct test_vals test_l[] = { 123/* 124 * In case of 64b system as the results of capital multipliers are multiplied 125 * by the sizeof(long) or sizeof(long long) respectively. 126 * Check "/lib/bytes_by_prefix.c" file for more information. 127 */ 128#if __SIZEOF_LONG__ == 8 129 {"552558m", 579399057408}, 130 {"5g", 5368709120}, 131 {"5K", 40960}, 132 {"5M", 41943040}, 133 {"1G", 8589934592} 134/* 135 * In case of 32b system as the results of capital multipliers are multiplied 136 * by the sizeof(long) or sizeof(long long) respectively. 137 * Check "/lib/bytes_by_prefix.c" file for more information. 138 */ 139#else 140 {"552558m", -1}, 141 {"5g", -1}, 142 {"5K", 20480}, 143 {"5M", 20971520}, 144 {"1G", -1} 145#endif 146}; 147 148/* 149 * Specific values for long long operations 150 */ 151struct test_vals test_ll[] = { 152 {"552558m", 579399057408LL}, 153 {"5g", 5368709120LL}, 154 {"5K", 40960}, 155 {"552558K", 4526555136LL}, 156 {"5M", 41943040}, 157 {"552558M", 4635192459264LL}, 158 {"5G", 42949672960LL}, 159 {"552558G", 4746437078286336LL} 160}; 161 162 163static int test_values(void) 164{ 165 /* 166 * 1st position of the array denotes the valid int operations 167 * 2nd position of the array denotes the valid long operations 168 * 3rd position of the array denotes the valid long long operations 169 */ 170 int valid_count[3]; 171 int tests_number[3]; /* int / long / long long */ 172 int i; 173 int error_count = 0; 174 int elements; /* Number of elements inside the test array. */ 175 176 elements = sizeof(test)/sizeof(struct test_vals); 177 /* 178 * Initializing counters. 179 */ 180 for (i = 0; i < 3; i++) { 181 valid_count[i] = 0; 182 tests_number[i] = elements; 183 } 184 185 /* 186 * The "generic" test loop. If the result of the function equals the 187 * expected predifined result, then increase the valid counter 188 */ 189 for (i = 0; i < elements; i++) { 190 if (bytes_by_prefix(test[i].val) == test[i].res) { 191 valid_count[0]++; 192 } else { 193 printf("Test value:%s failed on int.\n", test[i].val); 194 error_count++; 195 } 196 197 if (lbytes_by_prefix(test[i].val) == test[i].res) { 198 valid_count[1]++; 199 } else { 200 printf("Test value:%s failed on long.\n", test[i].val); 201 error_count++; 202 } 203 204 if (llbytes_by_prefix(test[i].val) == test[i].res) { 205 valid_count[2]++; 206 } else { 207 printf("Test value:%s failed on long long.\n", 208 test[i].val); 209 error_count++; 210 } 211 } 212 213 elements = sizeof(test_i)/sizeof(struct test_vals); 214 tests_number[0] += elements; 215 216 /* 217 * Int specific test loop 218 */ 219 for (i = 0; i < elements; i++) { 220 if (bytes_by_prefix(test_i[i].val) == test_i[i].res) { 221 valid_count[0]++; 222 } else { 223 printf("Test value:%s failed on int.\n", 224 test_i[i].val); 225 error_count++; 226 } 227 } 228 229 elements = sizeof(test_l)/sizeof(struct test_vals); 230 tests_number[1] += elements; 231 232 /* 233 * Long specific test loop 234 */ 235 for (i = 0; i < elements; i++) { 236 if (lbytes_by_prefix(test_l[i].val) == test_l[i].res) { 237 valid_count[1]++; 238 } else { 239 printf("Test value:%s failed on long.\n", 240 test_l[i].val); 241 error_count++; 242 } 243 } 244 245 elements = sizeof(test_ll)/sizeof(struct test_vals); 246 tests_number[2] += elements; 247 248 /* 249 * Long long specific test loop 250 */ 251 for (i = 0; i < elements; i++) { 252 if (llbytes_by_prefix(test_ll[i].val) == test_ll[i].res) { 253 valid_count[2]++; 254 } else { 255 printf("Test value:%s failed on long long.\n", 256 test_ll[i].val); 257 error_count++; 258 } 259 } 260 261 fprintf(stdout, "Succesfull int tests:%d/%d\n", valid_count[0], 262 tests_number[0]); 263 fprintf(stdout, "Succesfull long tests:%d/%d\n", valid_count[1], 264 tests_number[1]); 265 fprintf(stdout, "Succesfull long long tests:%d/%d\n", valid_count[2], 266 tests_number[2]); 267 268 return error_count; 269} 270 271int main(void) 272{ 273 int errors = test_values(); 274 275 if (errors > 0) { 276 fprintf(stderr, "\nSome test(s):(%d) failed please review!\n", 277 errors); 278 exit(1); 279 } else { 280 fprintf(stdout, "Tests succesfully completed!\n"); 281 } 282 283 return 0; 284} 285