1 /*- 2 * Copyright 2012 Clifton Royston 3 * Copyright 2013 John-Mark Gurney 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 * 29 */ 30 31 #include <sys/param.h> 32 #include <inttypes.h> 33 #include <libutil.h> 34 #include <limits.h> 35 #include <math.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 41 #define MAX_STR_FLAGS_RESULT 80 42 #define MAX_INT_STR_DIGITS 12 43 44 static const int64_t halfExabyte = (int64_t)500*1000*1000*1000*1000*1000L; 45 46 static struct { 47 int retval; 48 const char *res; 49 int64_t num; 50 int flags; 51 int scale; 52 } test_args[] = { 53 /* tests 0-13 test 1000 suffixes */ 54 { 2, "0 ", (int64_t)0L, HN_DIVISOR_1000, HN_AUTOSCALE }, 55 { 3, "1 k", (int64_t)500L, HN_DIVISOR_1000, HN_AUTOSCALE }, 56 { 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 57 { 3, "1 G", (int64_t)500*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 58 { 3, "1 T", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 59 { 3, "1 P", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 60 { 3, "1 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 61 { 2, "1 ", (int64_t)1L, HN_DIVISOR_1000, HN_AUTOSCALE }, 62 { 3, "2 k", (int64_t)1500L, HN_DIVISOR_1000, HN_AUTOSCALE }, 63 { 3, "2 M", (int64_t)1500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 64 { 3, "2 G", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 65 { 3, "2 T", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 66 { 3, "2 P", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 67 { 3, "2 E", (int64_t)1500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 68 69 /* tests 14-27 test 1024 suffixes */ 70 { 2, "0 ", (int64_t)0L, 0, HN_AUTOSCALE }, 71 { 3, "1 K", (int64_t)512L, 0, HN_AUTOSCALE }, 72 { 3, "1 M", (int64_t)512*1024L, 0, HN_AUTOSCALE }, 73 { 3, "1 G", (int64_t)512*1024*1024L, 0, HN_AUTOSCALE }, 74 { 3, "1 T", (int64_t)512*1024*1024*1024L, 0, HN_AUTOSCALE }, 75 { 3, "1 P", (int64_t)512*1024*1024*1024*1024L, 0, HN_AUTOSCALE }, 76 { 3, "1 E", (int64_t)512*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE }, 77 { 2, "1 ", (int64_t)1L, 0, HN_AUTOSCALE }, 78 { 3, "2 K", (int64_t)1536L, 0, HN_AUTOSCALE }, 79 { 3, "2 M", (int64_t)1536*1024L, 0, HN_AUTOSCALE }, 80 { 3, "2 G", (int64_t)1536*1024*1024L, 0, HN_AUTOSCALE }, 81 { 3, "2 T", (int64_t)1536*1024*1024*1024L, 0, HN_AUTOSCALE }, 82 { 3, "2 P", (int64_t)1536*1024*1024*1024*1024L, 0, HN_AUTOSCALE }, 83 { 3, "2 E", (int64_t)1536*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE }, 84 85 /* tests 28-37 test rounding */ 86 { 3, "0 M", (int64_t)500*1000L-1, HN_DIVISOR_1000, HN_AUTOSCALE }, 87 { 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 88 { 3, "1 M", (int64_t)1000*1000L + 500*1000L-1, HN_DIVISOR_1000, HN_AUTOSCALE }, 89 { 3, "2 M", (int64_t)1000*1000L + 500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 90 { 3, "0 K", (int64_t)512L-1, 0, HN_AUTOSCALE }, 91 { 3, "1 K", (int64_t)512L, 0, HN_AUTOSCALE }, 92 { 3, "0 M", (int64_t)512*1024L-1, 0, HN_AUTOSCALE }, 93 { 3, "1 M", (int64_t)512*1024L, 0, HN_AUTOSCALE }, 94 { 3, "1 M", (int64_t)1024*1024L + 512*1024L-1, 0, HN_AUTOSCALE }, 95 { 3, "2 M", (int64_t)1024*1024L + 512*1024L, 0, HN_AUTOSCALE }, 96 97 /* tests 38-61 test specific scale factors with 1000 divisor */ 98 { 3, "0 k", (int64_t)0L, HN_DIVISOR_1000, 1 }, 99 { 3, "1 k", (int64_t)500L, HN_DIVISOR_1000, 1 }, 100 { 3, "0 M", (int64_t)500L, HN_DIVISOR_1000, 2 }, 101 { 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, 2 }, 102 { 3, "0 G", (int64_t)500*1000L, HN_DIVISOR_1000, 3 }, 103 { 3, "1 G", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 3 }, 104 { 3, "0 T", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 4 }, 105 { 3, "1 T", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, 4 }, 106 { 3, "0 P", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, 5 }, 107 { 3, "1 P", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, 5 }, 108 { 3, "0 E", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 }, 109 { 3, "1 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 }, 110 { 3, "0 k", (int64_t)1L, HN_DIVISOR_1000, 1 }, 111 { 3, "2 k", (int64_t)1500L, HN_DIVISOR_1000, 1 }, 112 { 3, "0 M", (int64_t)1500L, HN_DIVISOR_1000, 2 }, 113 { 3, "2 M", (int64_t)1500*1000L, HN_DIVISOR_1000, 2 }, 114 { 3, "0 G", (int64_t)1500*1000L, HN_DIVISOR_1000, 3 }, 115 { 3, "2 G", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 3 }, 116 { 3, "0 T", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 4 }, 117 { 3, "2 T", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, 4 }, 118 { 3, "0 P", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, 5 }, 119 { 3, "2 P", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, 5 }, 120 { 3, "0 E", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 }, 121 { 3, "2 E", (int64_t)1500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 }, 122 123 /* tests 62-85 test specific scale factors with 1024 divisor */ 124 { 3, "0 K", (int64_t)0L, 0, 1 }, 125 { 3, "1 K", (int64_t)512L, 0, 1 }, 126 { 3, "0 M", (int64_t)512L, 0, 2 }, 127 { 3, "1 M", (int64_t)512*1024L, 0, 2 }, 128 { 3, "0 G", (int64_t)512*1024L, 0, 3 }, 129 { 3, "1 G", (int64_t)512*1024*1024L, 0, 3 }, 130 { 3, "0 T", (int64_t)512*1024*1024L, 0, 4 }, 131 { 3, "1 T", (int64_t)512*1024*1024*1024L, 0, 4 }, 132 { 3, "0 P", (int64_t)512*1024*1024*1024L, 0, 5 }, 133 { 3, "1 P", (int64_t)512*1024*1024*1024*1024L, 0, 5 }, 134 { 3, "0 E", (int64_t)512*1024*1024*1024*1024L, 0, 6 }, 135 { 3, "1 E", (int64_t)512*1024*1024*1024*1024*1024L, 0, 6 }, 136 { 3, "0 K", (int64_t)1L, 0, 1 }, 137 { 3, "2 K", (int64_t)1536L, 0, 1 }, 138 { 3, "0 M", (int64_t)1536L, 0, 2 }, 139 { 3, "2 M", (int64_t)1536*1024L, 0, 2 }, 140 { 3, "0 G", (int64_t)1536*1024L, 0, 3 }, 141 { 3, "2 G", (int64_t)1536*1024*1024L, 0, 3 }, 142 { 3, "0 T", (int64_t)1536*1024*1024L, 0, 4 }, 143 { 3, "2 T", (int64_t)1536*1024*1024*1024L, 0, 4 }, 144 { 3, "0 P", (int64_t)1536*1024*1024*1024L, 0, 5 }, 145 { 3, "2 P", (int64_t)1536*1024*1024*1024*1024L, 0, 5 }, 146 { 3, "0 E", (int64_t)1536*1024*1024*1024*1024L, 0, 6 }, 147 { 3, "2 E", (int64_t)1536*1024*1024*1024*1024*1024L, 0, 6 }, 148 149 /* tests 86-99 test invalid specific scale values of < 0 or >= 7 with 150 and without HN_DIVISOR_1000 set */ 151 /* all should return errors with new code; with old, the latter 3 152 are instead processed as if having AUTOSCALE and/or GETSCALE set */ 153 { -1, "", (int64_t)1L, 0, 7 }, 154 { -1, "", (int64_t)1L, HN_DIVISOR_1000, 7 }, 155 { -1, "", (int64_t)1L, 0, 1000 }, 156 { -1, "", (int64_t)1L, HN_DIVISOR_1000, 1000 }, 157 { -1, "", (int64_t)0L, 0, 1000*1000 }, 158 { -1, "", (int64_t)0L, HN_DIVISOR_1000, 1000*1000 }, 159 { -1, "", (int64_t)0L, 0, INT_MAX }, 160 { -1, "", (int64_t)0L, HN_DIVISOR_1000, INT_MAX }, 161 162 /* Negative scale values are not handled well 163 by the existing library routine - should report as error */ 164 /* all should return errors with new code, fail assertion with old */ 165 166 { -1, "", (int64_t)1L, 0, -1 }, 167 { -1, "", (int64_t)1L, HN_DIVISOR_1000, -1 }, 168 { -1, "", (int64_t)1L, 0, -1000 }, 169 { -1, "", (int64_t)1L, HN_DIVISOR_1000, -1000 }, 170 171 /* __INT_MIN doesn't print properly, skipped. */ 172 173 { -1, "", (int64_t)1L, 0, -__INT_MAX }, 174 { -1, "", (int64_t)1L, HN_DIVISOR_1000, -__INT_MAX }, 175 176 177 /* tests for scale == 0, without autoscale */ 178 /* tests 100-114 test scale 0 with 1000 divisor - print first N digits */ 179 { 2, "0 ", (int64_t)0L, HN_DIVISOR_1000, 0 }, 180 { 2, "1 ", (int64_t)1L, HN_DIVISOR_1000, 0 }, 181 { 3, "10 ", (int64_t)10L, HN_DIVISOR_1000, 0 }, 182 { 3, "0 M", (int64_t)150L, HN_DIVISOR_1000, HN_NOSPACE }, 183 { 3, "0 M", (int64_t)500L, HN_DIVISOR_1000, HN_NOSPACE }, 184 { 3, "0 M", (int64_t)999L, HN_DIVISOR_1000, HN_NOSPACE }, 185 { 4, "150", (int64_t)150L, HN_DIVISOR_1000, 0 }, 186 { 4, "500", (int64_t)500L, HN_DIVISOR_1000, 0 }, 187 { 4, "999", (int64_t)999L, HN_DIVISOR_1000, 0 }, 188 { 5, "100", (int64_t)1000L, HN_DIVISOR_1000, 0 }, 189 { 5, "150", (int64_t)1500L, HN_DIVISOR_1000, 0 }, 190 { 7, "500", (int64_t)500*1000L, HN_DIVISOR_1000, 0 }, 191 { 8, "150", (int64_t)1500*1000L, HN_DIVISOR_1000, 0 }, 192 { 10, "500", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 0 }, 193 { 11, "150", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 0 }, 194 195 /* tests 115-126 test scale 0 with 1024 divisor - print first N digits */ 196 { 2, "0 ", (int64_t)0L, 0, 0 }, 197 { 2, "1 ", (int64_t)1L, 0, 0 }, 198 { 3, "10 ", (int64_t)10L, 0, 0 }, 199 { 4, "150", (int64_t)150L, 0, 0 }, 200 { 4, "500", (int64_t)500L, 0, 0 }, 201 { 4, "999", (int64_t)999L, 0, 0 }, 202 { 5, "100", (int64_t)1000L, 0, 0 }, 203 { 5, "150", (int64_t)1500L, 0, 0 }, 204 { 7, "500", (int64_t)500*1000L, 0, 0 }, 205 { 8, "150", (int64_t)1500*1000L, 0, 0 }, 206 { 10, "500", (int64_t)500*1000*1000L, 0, 0 }, 207 { 11, "150", (int64_t)1500*1000*1000L, 0, 0 }, 208 209 /* Test boundary cases for very large positive/negative number formatting */ 210 /* Explicit scale, divisor 1024 */ 211 212 /* XXX = requires length 5 (buflen 6) for some cases*/ 213 /* KLUDGE - test loop below will bump length 5 up to 5 */ 214 { 3, "8 E", INT64_MAX, 0, 6 }, 215 { 4, "-8 E", -INT64_MAX, 0, 6 }, 216 { 3, "0 E", (int64_t)92*1024*1024*1024*1024*1024L, 0, 6 }, 217 { 3, "0 E", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 6 }, 218 { 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, 0, 6 }, 219 { 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 6 }, 220 { 3, "0 E", (int64_t)81*1024*1024*1024*1024*1024L, 0, 6 }, 221 { 3, "0 E", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 6 }, 222 { 4, "92 P", (int64_t)92*1024*1024*1024*1024*1024L, 0, 5 }, 223 { 5, "-92 P", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 5 }, 224 { 4, "82 P", (int64_t)82*1024*1024*1024*1024*1024L, 0, 5 }, 225 { 5, "-82 P", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 5 }, 226 { 4, "81 P", (int64_t)81*1024*1024*1024*1024*1024L, 0, 5 }, 227 { 5, "-81 P", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 5 }, 228 229 /* Explicit scale, divisor 1000 */ 230 { 3, "9 E", INT64_MAX, HN_DIVISOR_1000, 6 }, 231 { 4, "-9 E", -INT64_MAX, HN_DIVISOR_1000, 6 }, 232 { 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 6 }, 233 { 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 6 }, 234 { 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 6 }, 235 { 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 6 }, 236 { 4, "92 P", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 5 }, 237 { 5, "-92 P", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 5 }, 238 { 4, "91 P", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 5 }, 239 { 5, "-91 P", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 5 }, 240 241 /* Autoscale, divisor 1024 */ 242 { 3, "8 E", INT64_MAX, 0, HN_AUTOSCALE }, 243 { 4, "-8 E", -INT64_MAX, 0, HN_AUTOSCALE }, 244 { 4, "92 P", (int64_t)92*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE }, 245 { 5, "-92 P", -(int64_t)92*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE }, 246 { 4, "82 P", (int64_t)82*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE }, 247 { 5, "-82 P", -(int64_t)82*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE }, 248 { 4, "81 P", (int64_t)81*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE }, 249 { 5, "-81 P", -(int64_t)81*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE }, 250 /* Autoscale, divisor 1000 */ 251 { 3, "9 E", INT64_MAX, HN_DIVISOR_1000, HN_AUTOSCALE }, 252 { 4, "-9 E", -INT64_MAX, HN_DIVISOR_1000, HN_AUTOSCALE }, 253 { 4, "92 P", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE }, 254 { 5, "-92 P", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE }, 255 { 4, "91 P", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE }, 256 { 5, "-91 P", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE }, 257 258 /* 0 scale, divisor 1024 */ 259 { 12, "skdj", INT64_MAX, 0, 0 }, 260 { 21, "-9223", -INT64_MAX, 0, 0 }, 261 { 19, "10358", (int64_t)92*1024*1024*1024*1024*1024L, 0, 0 }, 262 { 20, "-1035", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 0 }, 263 { 18, "92323", (int64_t)82*1024*1024*1024*1024*1024L, 0, 0 }, 264 { 19, "-9232", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 0 }, 265 { 18, "91197", (int64_t)81*1024*1024*1024*1024*1024L, 0, 0 }, 266 { 19, "-9119", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 0 }, 267 268 /* 0 scale, divisor 1000 */ 269 /* XXX - why does this fail? */ 270 { -1, "", INT64_MAX, HN_DIVISOR_1000, 0 }, 271 { 21, "-9223", -INT64_MAX, HN_DIVISOR_1000, 0 }, 272 { 19, "10358", (int64_t)92*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0 }, 273 { 20, "-1035", -(int64_t)92*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0 }, 274 { 18, "92323", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0 }, 275 { 19, "-9232", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0 }, 276 /* Expected to pass */ 277 { 18, "91197", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0 }, 278 { 19, "-9119", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0 }, 279 280 281 282 /* Need to implement tests for GETSCALE */ 283 /* { ?, "", (int64_t)0L, HN_DIVISOR_1000, HN_GETSCALE }, 284 ... 285 */ 286 /* Tests for HN_DECIMAL */ 287 /* Positive, Autoscale */ 288 { 5, "500 k", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 289 { 5, "994 k", (int64_t)994*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 290 { 5, "995 k", (int64_t)995*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 291 { 5, "999 k", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 292 { 5, "1.0 M", (int64_t)1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 293 { 5, "1.5 M", (int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 294 { 5, "1.9 M", (int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 295 { 5, "2.0 M", (int64_t)1950*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 296 { 5, "9.9 M", (int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 297 { 4, "10 M", (int64_t)9950*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 298 { 5, "500 M", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 299 { 5, "994 M", (int64_t)994*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 300 { 5, "995 M", (int64_t)995*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 301 { 5, "999 M", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 302 303 { 5, "500 K", (int64_t)500*1024L, HN_DECIMAL, HN_AUTOSCALE }, 304 { 5, "994 K", (int64_t)994*1024L, HN_DECIMAL, HN_AUTOSCALE }, 305 { 5, "995 K", (int64_t)995*1024L, HN_DECIMAL, HN_AUTOSCALE }, 306 { 5, "999 K", (int64_t)999*1024L, HN_DECIMAL, HN_AUTOSCALE }, 307 { 5, "1.0 M", (int64_t)1000*1024L, HN_DECIMAL, HN_AUTOSCALE }, 308 { 5, "1.0 M", (int64_t)1018*1024L, HN_DECIMAL, HN_AUTOSCALE }, 309 { 5, "1.0 M", (int64_t)1019*1024L, HN_DECIMAL, HN_AUTOSCALE }, 310 { 5, "1.5 M", (int64_t)1536*1024L, HN_DECIMAL, HN_AUTOSCALE }, 311 { 5, "1.9 M", (int64_t)1996*1024L, HN_DECIMAL, HN_AUTOSCALE }, 312 { 5, "2.0 M", (int64_t)1997*1024L, HN_DECIMAL, HN_AUTOSCALE }, 313 { 5, "2.0 M", (int64_t)2047*1024L, HN_DECIMAL, HN_AUTOSCALE }, 314 { 5, "2.0 M", (int64_t)2048*1024L, HN_DECIMAL, HN_AUTOSCALE }, 315 { 5, "2.0 M", (int64_t)2099*1024L, HN_DECIMAL, HN_AUTOSCALE }, 316 { 5, "2.1 M", (int64_t)2100*1024L, HN_DECIMAL, HN_AUTOSCALE }, 317 { 5, "9.9 M", (int64_t)10188*1024L, HN_DECIMAL, HN_AUTOSCALE }, 318 /* XXX - shouldn't the following two be "10. M"? */ 319 { 4, "10 M", (int64_t)10189*1024L, HN_DECIMAL, HN_AUTOSCALE }, 320 { 4, "10 M", (int64_t)10240*1024L, HN_DECIMAL, HN_AUTOSCALE }, 321 { 5, "500 M", (int64_t)500*1024*1024L, HN_DECIMAL, HN_AUTOSCALE }, 322 { 5, "994 M", (int64_t)994*1024*1024L, HN_DECIMAL, HN_AUTOSCALE }, 323 { 5, "995 M", (int64_t)995*1024*1024L, HN_DECIMAL, HN_AUTOSCALE }, 324 { 5, "999 M", (int64_t)999*1024*1024L, HN_DECIMAL, HN_AUTOSCALE }, 325 { 5, "1.0 G", (int64_t)1000*1024*1024L, HN_DECIMAL, HN_AUTOSCALE }, 326 { 5, "1.0 G", (int64_t)1023*1024*1024L, HN_DECIMAL, HN_AUTOSCALE }, 327 328 /* Negative, Autoscale - should pass */ 329 { 6, "-1.5 ", -(int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 330 { 6, "-1.9 ", -(int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 331 { 6, "-9.9 ", -(int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 332 333 { 6, "-1.5 ", -(int64_t)1536*1024L, HN_DECIMAL, HN_AUTOSCALE }, 334 { 6, "-1.9 ", -(int64_t)1949*1024L, HN_DECIMAL, HN_AUTOSCALE }, 335 { 6, "-9.7 ", -(int64_t)9949*1024L, HN_DECIMAL, HN_AUTOSCALE }, 336 337 /* Positive/negative, at maximum scale */ 338 { 5, "500 P", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 339 { 5, "1.9 E", (int64_t)1949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 340 { 5, "8.9 E", (int64_t)8949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE }, 341 { 5, "9.2 E", INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 342 /* Negatives work with latest rev only: */ 343 { 6, "-9.2 ", -INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 344 { 6, "-8.9 ", -(int64_t)8949*1000*1000*1000*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE }, 345 346 { 5, "8.0 E", INT64_MAX, HN_DECIMAL, HN_AUTOSCALE }, 347 { 5, "7.9 E", INT64_MAX-(int64_t)100*1024*1024*1024*1024*1024LL, HN_DECIMAL, HN_AUTOSCALE }, 348 { 6, "-8.0 ", -INT64_MAX, HN_DECIMAL, HN_AUTOSCALE }, 349 { 6, "-7.9 ", -INT64_MAX+(int64_t)100*1024*1024*1024*1024*1024LL, HN_DECIMAL, HN_AUTOSCALE }, 350 351 /* Positive, Fixed scales */ 352 { 5, "500 k", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 }, 353 { 5, "0.5 M", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 354 { 5, "949 k", (int64_t)949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 }, 355 { 5, "0.9 M", (int64_t)949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 356 { 5, "950 k", (int64_t)950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 }, 357 { 5, "1.0 M", (int64_t)950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 358 { 5, "999 k", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 }, 359 { 5, "1.0 M", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 360 { 5, "1.5 M", (int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 361 { 5, "1.9 M", (int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 362 { 5, "2.0 M", (int64_t)1950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 363 { 5, "9.9 M", (int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 364 { 4, "10 M", (int64_t)9950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 365 { 5, "500 M", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 366 { 5, "0.5 G", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 3 }, 367 { 5, "999 M", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 }, 368 { 5, "1.0 G", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 3 }, 369 /* Positive/negative, at maximum scale */ 370 { 5, "500 P", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 5 }, 371 { 5, "1.0 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 }, 372 { 5, "1.9 E", (int64_t)1949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 }, 373 { 5, "8.9 E", (int64_t)8949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 }, 374 { 5, "9.2 E", INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, 6 }, 375 376 /* HN_DECIMAL + binary + fixed scale cases not completed */ 377 { 5, "512 K", (int64_t)512*1024L, HN_DECIMAL, 1 }, 378 { 5, "0.5 M", (int64_t)512*1024L, HN_DECIMAL, 2 }, 379 380 /* Negative, Fixed scales */ 381 /* Not yet added, but should work with latest rev */ 382 383 }; 384 385 386 /* Command line options usage */ 387 static void 388 usage(char * progname) { 389 printf("%s: tests libutil humanize_number function\n", progname); 390 printf("Usage: %s [-nE] [-l num] [-v]\n\n", progname); 391 printf("Options:\n"); 392 printf("\t-l num\tSet max length for result; buflen = num + 1\n"); 393 printf("\t\t (NOTE: does not change expected result strings.)\n"); 394 printf("\t-n\tInclude negative scale tests, which cause older libutil\n"); 395 printf("\t\t version of function to coredump with assertion failure\n"); 396 printf("\t-E\tInclude numbers > 1/2 Exa[byte] which currently fail\n"); 397 printf("\t-v\tVerbose - always print summary results\n"); 398 printf("\t-h, -?\tShow options\n"); 399 } 400 401 /* Parse command line options */ 402 static void 403 read_options(int argc, char * const argv[], size_t *bufLength, 404 int *includeNegativeScale, int *includeExabytes, int *verbose) { 405 int ch; 406 size_t temp; 407 408 while ((ch = getopt(argc, argv, "nEh?vl:")) != -1) { 409 switch (ch) { 410 default: 411 usage(argv[0]); 412 exit(1); 413 break; /* UNREACHABLE */ 414 case 'h' : 415 case '?' : 416 usage(argv[0]); 417 exit(0); 418 break; /* UNREACHABLE */ 419 case 'l' : 420 sscanf(optarg, "%zu", &temp); 421 *bufLength = temp + 1; 422 break; 423 case 'n' : 424 *includeNegativeScale = 1; 425 break; 426 case 'E' : 427 *includeExabytes = 1; 428 break; 429 case 'v' : 430 *verbose = 1; 431 break; 432 } 433 } 434 } 435 436 static struct { 437 int value; 438 const char *name; 439 } flags[] = { 440 { HN_AUTOSCALE, "HN_AUTOSCALE" }, 441 { HN_GETSCALE, "HN_GETSCALE" }, 442 { HN_DIVISOR_1000, "HN_DIVISOR_1000"}, 443 { HN_B, "HN_B"}, 444 { HN_DECIMAL, "HN_DECIMAL"}, 445 }; 446 447 static const char *separator = "|"; 448 449 /* Format flags parameter for meaningful display */ 450 static char * 451 str_flags(int hn_flags, char *noFlags) { 452 size_t i; 453 char * result; 454 455 result = malloc(MAX_STR_FLAGS_RESULT); 456 result[0] = '\0'; 457 458 for (i = 0; i < sizeof flags / sizeof *flags; i++) { 459 if (hn_flags & flags[i].value) { 460 if (*result != 0) 461 strlcat(result, separator, 462 MAX_STR_FLAGS_RESULT); 463 strlcat(result, flags[i].name, MAX_STR_FLAGS_RESULT); 464 } 465 } 466 467 if (strlen(result) == 0) 468 strlcat(result, noFlags, MAX_STR_FLAGS_RESULT); 469 return result; 470 } 471 472 473 /* Format scale parameter for meaningful display */ 474 static char * 475 str_scale(int scale) { 476 char *result; 477 478 if (scale == HN_AUTOSCALE || scale == HN_GETSCALE) 479 return str_flags(scale, ""); 480 481 result = malloc(MAX_INT_STR_DIGITS); 482 result[0] = '\0'; 483 snprintf(result, MAX_INT_STR_DIGITS, "%d", scale); 484 return result; 485 } 486 487 static void 488 testskipped(size_t i) 489 { 490 491 printf("ok %zu # skip - not turned on\n", i); 492 } 493 494 int 495 main(int argc, char * const argv[]) 496 { 497 char *buf; 498 char *flag_str, *scale_str; 499 size_t buflen, errcnt, i, skipped, tested; 500 int r; 501 int includeNegScale; 502 int includeExabyteTests; 503 int verbose; 504 505 buflen = 4; 506 includeNegScale = 0; 507 includeExabyteTests = 0; 508 verbose = 0; 509 510 read_options(argc, argv, &buflen, &includeNegScale, 511 &includeExabyteTests, &verbose); 512 513 buf = malloc(buflen); 514 errcnt = 0; 515 tested = 0; 516 skipped = 0; 517 518 if (buflen != 4) 519 printf("Warning: buffer size %zu != 4, expect some results to differ.\n", buflen); 520 521 printf("1..%zu\n", nitems(test_args)); 522 for (i = 0; i < nitems(test_args); i++) { 523 /* KLUDGE */ 524 if (test_args[i].num == INT64_MAX && buflen == 4) { 525 /* Start final tests which require buffer of 6 */ 526 free(buf); 527 buflen = 6; 528 buf = malloc(buflen); 529 if (verbose) 530 printf("Buffer length increased to %zu\n", 531 buflen); 532 } 533 534 if (test_args[i].scale < 0 && ! includeNegScale) { 535 skipped++; 536 testskipped(i + 1); 537 continue; 538 } 539 if (test_args[i].num >= halfExabyte && ! includeExabyteTests) { 540 skipped++; 541 testskipped(i + 1); 542 continue; 543 } 544 545 r = humanize_number(buf, buflen, test_args[i].num, "", 546 test_args[i].scale, test_args[i].flags); 547 flag_str = str_flags(test_args[i].flags, "[no flags]"); 548 scale_str = str_scale(test_args[i].scale); 549 550 if (r != test_args[i].retval) { 551 if (verbose) 552 printf("wrong return value on index %zu, " 553 "buflen: %zu, got: %d + \"%s\", " 554 "expected %d + \"%s\"; num = %jd, " 555 "scale = %s, flags= %s.\n", 556 i, buflen, r, buf, test_args[i].retval, 557 test_args[i].res, 558 (intmax_t)test_args[i].num, 559 scale_str, flag_str); 560 else 561 printf("not ok %zu # return %d != %d\n", 562 i + 1, r, test_args[i].retval); 563 errcnt++; 564 } else if (strcmp(buf, test_args[i].res) != 0) { 565 if (verbose) 566 printf("result mismatch on index %zu, got: " 567 "\"%s\", expected \"%s\"; num = %jd, " 568 "scale = %s, flags= %s.\n", 569 i, buf, test_args[i].res, 570 (intmax_t)test_args[i].num, 571 scale_str, flag_str); 572 else 573 printf("not ok %zu # buf \"%s\" != \"%s\"\n", 574 i + 1, buf, test_args[i].res); 575 errcnt++; 576 } else { 577 if (verbose) 578 printf("successful result on index %zu, " 579 "returned %d, got: \"%s\"; num = %jd, " 580 "scale = %s, flags= %s.\n", 581 i, r, buf, 582 (intmax_t)test_args[i].num, 583 scale_str, flag_str); 584 else 585 printf("ok %zu\n", i + 1); 586 } 587 tested++; 588 } 589 590 if (verbose) 591 printf("total errors: %zu/%zu tests, %zu skipped\n", errcnt, 592 tested, skipped); 593 594 if (errcnt) 595 return 1; 596 597 return 0; 598 } 599