| 1 | /* |
| 2 | * Copyright (C) - 2013 Simon Marchi <simon.marchi@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by as |
| 6 | * published by the Free Software Foundation; only version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <string.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #include <tap/tap.h> |
| 23 | |
| 24 | #include <common/utils.h> |
| 25 | |
| 26 | /* For error.h */ |
| 27 | int lttng_opt_quiet = 1; |
| 28 | int lttng_opt_verbose = 3; |
| 29 | int lttng_opt_mi; |
| 30 | |
| 31 | struct valid_test_input { |
| 32 | char *input; |
| 33 | uint64_t expected_result; |
| 34 | }; |
| 35 | |
| 36 | /* Valid test cases */ |
| 37 | static struct valid_test_input valid_tests_inputs[] = { |
| 38 | { "0", 0 }, |
| 39 | { "1234", 1234 }, |
| 40 | { "0x400", 1024 }, |
| 41 | { "0300", 192 }, |
| 42 | { "16k", 16384 }, |
| 43 | { "128K", 131072 }, |
| 44 | { "0x1234k", 4771840 }, |
| 45 | { "32M", 33554432 }, |
| 46 | { "1024G", 1099511627776ULL }, |
| 47 | { "0X400", 1024 }, |
| 48 | { "0x40a", 1034 }, |
| 49 | { "0X40b", 1035 }, |
| 50 | { "0x40C", 1036 }, |
| 51 | { "0X40D", 1037 }, |
| 52 | { "0x40e", 1038 }, |
| 53 | { "0X40f", 1039 }, |
| 54 | { "00", 0 }, |
| 55 | { "0k", 0 }, |
| 56 | { "0K", 0 }, |
| 57 | { "0M", 0 }, |
| 58 | { "0G", 0 }, |
| 59 | { "00k", 0 }, |
| 60 | { "00K", 0 }, |
| 61 | { "00M", 0 }, |
| 62 | { "00G", 0 }, |
| 63 | { "0x0", 0 }, |
| 64 | { "0X0", 0 }, |
| 65 | { "0x0k", 0 }, |
| 66 | { "0X0K", 0 }, |
| 67 | { "0x0M", 0 }, |
| 68 | { "0X0G", 0 }, |
| 69 | { "0X40G", 68719476736ULL }, |
| 70 | { "0300k", 196608 }, |
| 71 | { "0300K", 196608 }, |
| 72 | { "030M", 25165824 }, |
| 73 | { "020G", 17179869184ULL }, |
| 74 | { "0xa0k", 163840 }, |
| 75 | { "0xa0K", 163840 }, |
| 76 | { "0XA0M", 167772160 }, |
| 77 | { "0xA0G", 171798691840ULL }, |
| 78 | }; |
| 79 | static const int num_valid_tests = sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]); |
| 80 | |
| 81 | /* Invalid test cases */ |
| 82 | static char *invalid_tests_inputs[] = { |
| 83 | "", |
| 84 | " ", |
| 85 | "-1", |
| 86 | "k", |
| 87 | "4611686018427387904G", |
| 88 | "0x40g", |
| 89 | "08", |
| 90 | "09", |
| 91 | "0x", |
| 92 | "x0", |
| 93 | "0xx0", |
| 94 | "07kK", |
| 95 | "0xk", |
| 96 | "0XM", |
| 97 | "0xG", |
| 98 | "0x0MM", |
| 99 | "0X0GG", |
| 100 | "0a", |
| 101 | "0B", |
| 102 | }; |
| 103 | |
| 104 | static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); |
| 105 | |
| 106 | static void test_utils_parse_size_suffix(void) |
| 107 | { |
| 108 | uint64_t result; |
| 109 | int ret; |
| 110 | int i; |
| 111 | |
| 112 | /* Test valid cases */ |
| 113 | for (i = 0; i < num_valid_tests; i++) { |
| 114 | char name[100]; |
| 115 | sprintf(name, "valid test case: %s", valid_tests_inputs[i].input); |
| 116 | |
| 117 | ret = utils_parse_size_suffix(valid_tests_inputs[i].input, &result); |
| 118 | ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name); |
| 119 | } |
| 120 | |
| 121 | /* Test invalid cases */ |
| 122 | for (i = 0; i < num_invalid_tests; i++) { |
| 123 | char name[100]; |
| 124 | sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]); |
| 125 | |
| 126 | ret = utils_parse_size_suffix(invalid_tests_inputs[i], &result); |
| 127 | ok(ret != 0, name); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | int main(int argc, char **argv) |
| 132 | { |
| 133 | plan_tests(num_valid_tests + num_invalid_tests); |
| 134 | |
| 135 | diag("utils_parse_size_suffix tests"); |
| 136 | |
| 137 | test_utils_parse_size_suffix(); |
| 138 | |
| 139 | return exit_status(); |
| 140 | } |