1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
|
#include "wvtest.h"
#include "wvfile.h"
#include "strutils.h"
#include "wvlinklist.h"
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <stdio.h>
/**
* Functions in strutils.h left untested:
* hexdump_buffer
* isnewline
* rfc822_date
* rfc1123_date
* passwd_crypt
*
* Tests that may be incorrect:
* fqdomainname
*/
/** Tests terminate_string().
* terminate_string() should remove any trailing whitespace on the
* incoming string and append the provided char.
*/
WVTEST_MAIN("terminate_string")
{
char *input[] = {new char[6], new char[7], new char[2], new char[4]};
strcpy(input[0], "blah"); strcpy(input[1], "blah\n");
strcpy(input[2], ""); strcpy(input[3], "\r\n");
const char *desired[] = {"blah!", "blah!", "!", "!"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
char *result = terminate_string(input[i], '!');
if (!WVFAIL(strcmp(result, desired[i])))
printf(" because [%s] != [%s]\n", result, desired[i]);
deletev input[i];
}
}
/** Tests trim_string().
* trim_string() should remove any leading or trailing whitespace from
* the incoming string.
*/
WVTEST_MAIN("trim")
{
char *input[] = {new char[7], new char[10],
new char[10], new char[10], new char[8]};
strcpy(input[0], "foobar"); strcpy(input[1], "\t foobar");
strcpy(input[2], "foobar\n "); strcpy(input[3], " \tfoo\r\t \n");
strcpy(input[4], "foo bar");
const char *desired[] = {"foobar", "foobar", "foobar", "foo", "foo bar"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
char *result = trim_string(input[i]);
if (!WVFAIL(strcmp(result, desired[i])))
printf(" because [%s] != [%s]\n", result, desired[i]);
deletev input[i];
}
}
/** Tests trim_string().
* trim_string(), when provided with a trim character, should return
* the portion of the incoming string that is before the first
* occurence of that character.
*/
WVTEST_MAIN("trimtest2.cc")
{
char *input[] = {new char[9], new char[9], new char[9], new char[9]};
strcpy(input[0], "abcdefgh"); strcpy(input[1], "xbcdefgh");
strcpy(input[2], "abcdefgx"); strcpy(input[3], "abcxefgh");
const char *desired[] = {"abcdefgh", "", "abcdefg", "abc"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
char *result = trim_string(input[i], 'x');
if (!WVFAIL(strcmp(result, desired[i])))
printf(" because [%s] != [%s]\n", result, desired[i]);
deletev input[i];
}
}
/** Tests non_breaking().
* non_breaking() should replace any whitespace character in the
* incoming string with " ".
*/
WVTEST_MAIN("nbsp")
{
const char *input[] = {"a b c", " a", "a\nb\tc ", "ab c\r"};
const char *desired[] = {"a b c", " a", "a b c ", "ab c "};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
char *result = non_breaking(input[i]);
if (!WVFAIL(strcmp(result, desired[i])))
printf(" because [%s] != [%s]\n", result, desired[i]);
deletev result;
}
}
/** Tests replace_char().
* replace_char() should replace all instances of one given character
* with another given character.
*/
WVTEST_MAIN("replace_char")
{
char *input[] = {new char[9], new char[9], new char[9], new char[9]};
strcpy(input[0], "rbcdefgh"); strcpy(input[1], "abrdergh");
strcpy(input[2], "abrdergr"); strcpy(input[3], "arrdefrh");
const char *desired[] = {"xbcdefgh", "abxdergh", "abxdexgx", "axxdefrh"};
const int len[] = {8, 5, 8, 5};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
replace_char((void *)input[i], 'r', 'x', len[i]);
if (!WVFAIL(strcmp(input[i], desired[i])))
printf(" because [%s] != [%s]\n", input[i], desired[i]);
deletev input[i];
}
}
/** Tests snip_string().
* snip_string() should snip a given input string A from another string
* B iff A is a prefix of B.
*/
WVTEST_MAIN("snip")
{
const char *input[] = {"foomatic", "automatic", "mafootic", " foobar"};
const char *desired[] = {"matic", "automatic", "mafootic", " foobar"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
char *result = snip_string((char*)input[i], (char*)"foo");
if (!WVFAIL(strcmp(result, desired[i])))
printf(" because [%s] != [%s]\n", result, desired[i]);
}
}
/** Tests strlwr().
* strlwr() should convert all characters in an input string to lower case.
*/
WVTEST_MAIN("strlwr")
{
char *input[] = {new char[6], new char[6], new char[6], new char[6]};
strcpy(input[0], "AbcdE"); strcpy(input[1], "aB De");
strcpy(input[2], "a@C^e"); strcpy(input[3], "\tB\ndE");
const char *desired[] = {"abcde", "ab de", "a@c^e", "\tb\nde"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
char *result = strlwr(input[i]);
if (!WVFAIL(strcmp(result, desired[i])))
printf(" because [%s] != [%s]\n", result, desired[i]);
deletev input[i];
}
WVPASS(strlwr(NULL) == NULL);
}
/** Tests strupr().
* strupr() should convert all characters in an input string to upper case.
*/
WVTEST_MAIN("strupr")
{
char *input[] = {new char[6], new char[6], new char[6], new char[6]};
strcpy(input[0], "aBCDe"); strcpy(input[1], "Ab dE");
strcpy(input[2], "A@c^E"); strcpy(input[3], "\tb\nDe");
const char *desired[] = {"ABCDE", "AB DE", "A@C^E", "\tB\nDE"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
char *result = strupr(input[i]);
if (!WVFAIL(strcmp(result, desired[i])))
printf(" because [%s] != [%s]\n", result, desired[i]);
deletev input[i];
}
WVPASS(strupr(NULL) == NULL);
}
/** Tests is_word().
* is_word() should return whether or not all the characters in an
* input string are alphanumeric (ie, the string is a 'word').
*/
WVTEST_MAIN("is_word")
{
const char *input[] = {"q1w2e3", "q!w@e#", "Q 86", "\t\n\r52", "hy-phen"};
const bool desired[] = {true, false, false, false, false};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
WVPASS(is_word(input[i]) == desired[i]);
}
/** Tests url_decode().
* url_decode() should convert all url-encoded characters in an input
* string (%xx) to their corresponding ASCII characters.
*/
WVTEST_MAIN("url_decode")
{
const char *input = "%49+%6c%69%6b%65+%70%69%7a%7a%61%21";
const char* desired = "I like pizza!";
WVPASSEQ(url_decode(input), desired);
const char *c = "";
WVPASSEQ(url_decode(c), "");
WvString x;
WVFAIL(url_decode(x));
}
/** Tests url_encode().
* url_encode() should convert all appropriate ASCII characters to
* their url-encoded equivalent.
*/
WVTEST_MAIN("url_encode")
{
const char *input = "http://www.free_email-account.com/~ponyman/mail.pl?name=\'to|\\|Y |)4|\\|Z4\'&pass=$!J83*p&folder=1N8()><random%";
const char *desired = "http%3A%2F%2Fwww.free_email-account.com%2F~ponyman%2Fmail.pl%3Fname%3D'to%7C%5C%7CY%20%7C)4%7C%5C%7CZ4'%26pass%3D%24!J83*p%26folder%3D1N8()%3E%3Crandom%25";
const char *slash_desired = "http:%2F%2Fwww.free_email-account.com%2F~ponyman%2Fmail.pl?name=\'to|\\|Y |)4|\\|Z4\'&pass=$!J83*p&folder=1N8()><random%25";
WVPASSEQ(url_encode(input), desired);
// for this next test, % should be escaped implicitly
WVPASSEQ(url_encode(input, "/"), slash_desired);
const char *c = "";
WVPASSEQ(url_encode(c), "");
WvString x;
WVFAIL(url_encode(x));
}
/** Tests backslash_escape().
* backslash_escape() should escape all non-alphanumeric characters
* with a leading backslash.
*/
WVTEST_MAIN("backslash_escape")
{
const char *input[] = {"hoopla!", "q!w2e3r$", "_+:\"<>?\\/", "J~0|<3R"};
const char *desired[] = {"hoopla\\!", "q\\!w2e3r\\$", "\\_\\+\\:\\\"\\<\\>\\?\\\\\\/", "J\\~0\\|\\<3R"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
WvString result = backslash_escape(input[i]);
if (!WVPASS(result == desired[i]))
printf(" because [%s] != [%s]\n", result.cstr(), desired[i]);
}
}
/** Tests strcount().
* strcount() should return the number of occurences of a given
* character in the input string.
*/
WVTEST_MAIN("strcount")
{
const char *input[] = {"abj;lewi", "lk327ga", "a87gai783a", "aaaaaaa", "ao8&ATO@a"};
int desired[] = {1, 1, 3, 7, 2};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
WVPASS(strcount(input[i], 'a') == desired[i]);
}
/** Tests encode_hostname_as_DN().
* encode_hostname_as_DN should return a DName with dc components
* containing each part of the given URI (host, domain, suffix, etc.)
* and a cn component containing the entire URI.
*/
WVTEST_MAIN("encode_hostname")
{
const char *input[] = {"www.service.net", "www.you-can-too.com",
"happybirthday.org", "www.canada.bigco.co.uk"};
WvString desired[] = {"dc=www,dc=service,dc=net,cn=www.service.net", "dc=www,dc=you-can-too,dc=com,cn=www.you-can-too.com", "dc=happybirthday,dc=org,cn=happybirthday.org", "dc=www,dc=canada,dc=bigco,dc=co,dc=uk,cn=www.canada.bigco.co.uk"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
WVPASS(encode_hostname_as_DN(input[i]) == desired[i]);
}
/** Tests nice_hostname().
* nice_hostname() should replace underscores with hyphens, removing
* duplicates, and remove any invalid (for a URI) characters, unless
* they are the first character in the string, in which case they
* should be replaced by 'x'.
*/
WVTEST_MAIN("nice_hostname")
{
const char *input[] = {"n-i_c-e", "@2COOL", "E\\/1|_.1", "ha--ha__ha"};
WvString desired[] = {"n-i-c-e", "x2COOL", "E1-.1", "ha-ha-ha"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
WVPASS(nice_hostname(input[i]) == desired[i]);
}
/** Tests getfilename().
* getfilename() should return the bottom-most entry in a given
* filename.
*/
WVTEST_MAIN("getfilename")
{
const char *input[] = {"/tmp/file", "file.ext", "../../.file.wot",
"/snick/dir/", "/snick/dira/../dirb/file"};
WvString desired[] = {"file", "file.ext", ".file.wot", "dir", "file"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
WVPASS(getfilename(input[i]) == desired[i]);
}
/** Tests getdirname().
* getdirname() should return the directory (hypothetically) containing
* the bottom-most entry in a specified filename.
*/
WVTEST_MAIN("getdirname")
{
const char *input[] = {"/tmp/file", "file.ext", "../../.file.wot",
"/snick/dir/", "/snick/dira/../dirb/file"};
WvString desired[] = {"/tmp", ".", "../..", "/snick", "/snick/dira/../dirb"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
WVPASS(getdirname(input[i]) == desired[i]);
}
/** Tests sizetoa().
* sizetoa() should return an appropriate text description of the size
* of a given number of blocks with a given blocksize.
*/
WVTEST_MAIN("sizetoa simple")
{
// Check various blocksizes
{
long blocks = 987654321;
long blocksize = 1000000;
const char *desired[15] = {"987.7 TB", "98.8 TB", "9.9 TB", "987.7 GB",
"98.8 GB", "9.9 GB", "987.7 MB", "98.8 MB",
"9.9 MB", "987.7 kB", "98.8 kB", "9.9 kB",
"987 bytes", "98 bytes", "9 bytes"};
int i = 0;
while(blocksize != 1)
{
WVPASSEQ(sizetoa(blocks, blocksize), desired[i]);
blocksize /= 10;
i++;
}
while(blocks)
{
WVPASSEQ(sizetoa(blocks, blocksize), desired[i]);
blocks /= 10;
i++;
}
}
// Now check rounding
WVPASSEQ(sizetoa(0), "0 bytes");
WVPASSEQ(sizetoa(1), "1 bytes");
WVPASSEQ(sizetoa(42), "42 bytes");
WVPASSEQ(sizetoa(666), "666 bytes");
WVPASSEQ(sizetoa(949), "949 bytes");
WVPASSEQ(sizetoa(999), "999 bytes");
WVPASSEQ(sizetoa(1000), "1.0 kB");
WVPASSEQ(sizetoa(1049), "1.0 kB");
WVPASSEQ(sizetoa(1050), "1.1 kB");
WVPASSEQ(sizetoa(1051), "1.1 kB");
WVPASSEQ(sizetoa(1099), "1.1 kB");
WVPASSEQ(sizetoa(1999), "2.0 kB");
WVPASSEQ(sizetoa(949999), "950.0 kB");
WVPASSEQ(sizetoa(999999), "1.0 MB");
WVPASSEQ(sizetoa(1000000), "1.0 MB");
WVPASSEQ(sizetoa(1049999), "1.0 MB");
WVPASSEQ(sizetoa(1050000), "1.1 MB");
WVPASSEQ(sizetoa(1050001), "1.1 MB");
WVPASSEQ(sizetoa(1099999), "1.1 MB");
WVPASSEQ(sizetoa(1999999), "2.0 MB");
WVPASSEQ(sizetoa(949999999), "950.0 MB");
WVPASSEQ(sizetoa(999999999), "1.0 GB");
WVPASSEQ(sizetoa(1000000000), "1.0 GB");
WVPASSEQ(sizetoa(1049999999), "1.0 GB");
WVPASSEQ(sizetoa(1050000000), "1.1 GB");
WVPASSEQ(sizetoa(1050000001), "1.1 GB");
WVPASSEQ(sizetoa(1099999999), "1.1 GB");
WVPASSEQ(sizetoa(1999999999), "2.0 GB");
WVPASSEQ(sizetoa(4294967295ul), "4.3 GB");
WVPASSEQ(sizetoa(949999999999ull), "950.0 GB");
WVPASSEQ(sizetoa(999999999999ull), "1.0 TB");
WVPASSEQ(sizetoa(1000000000000ull), "1.0 TB");
WVPASSEQ(sizetoa(1049999999999ull), "1.0 TB");
WVPASSEQ(sizetoa(1050000000000ull), "1.1 TB");
WVPASSEQ(sizetoa(1050000000001ull), "1.1 TB");
WVPASSEQ(sizetoa(949999999999999ull), "950.0 TB");
WVPASSEQ(sizetoa(999999999999999ull), "1.0 PB");
WVPASSEQ(sizetoa(1000000000000000ull), "1.0 PB");
WVPASSEQ(sizetoa(1049999999999999ull), "1.0 PB");
WVPASSEQ(sizetoa(1050000000000000ull), "1.1 PB");
WVPASSEQ(sizetoa(1050000000000001ull), "1.1 PB");
WVPASSEQ(sizetoa(949999999999999999ull), "950.0 PB");
WVPASSEQ(sizetoa(999999999999999999ull), "1.0 EB");
WVPASSEQ(sizetoa(1000000000000000000ull), "1.0 EB");
WVPASSEQ(sizetoa(1049999999999999999ull), "1.0 EB");
WVPASSEQ(sizetoa(1050000000000000000ull), "1.1 EB");
WVPASSEQ(sizetoa(1050000000000000001ull), "1.1 EB");
WVPASSEQ(sizetoa(18446744073709551615ull), "18.4 EB");
WVPASSEQ(sizetoa(0, 1000), "0 bytes");
WVPASSEQ(sizetoa(1, 1000), "1.0 kB");
WVPASSEQ(sizetoa(42, 1000), "42.0 kB");
WVPASSEQ(sizetoa(666, 1000), "666.0 kB");
WVPASSEQ(sizetoa(949, 1000), "949.0 kB");
WVPASSEQ(sizetoa(999, 1000), "999.0 kB");
WVPASSEQ(sizetoa(949999999999999999ull, 1000), "950.0 EB");
WVPASSEQ(sizetoa(999999999999999999ull, 1000), "1.0 ZB");
WVPASSEQ(sizetoa(1000000000000000000ull, 1000), "1.0 ZB");
WVPASSEQ(sizetoa(1049999999999999999ull, 1000), "1.0 ZB");
WVPASSEQ(sizetoa(1050000000000000000ull, 1000), "1.1 ZB");
WVPASSEQ(sizetoa(1050000000000000001ull, 1000), "1.1 ZB");
WVPASSEQ(sizetoa(1099999999999999999ull, 1000), "1.1 ZB");
WVPASSEQ(sizetoa(1999999999999999999ull, 1000), "2.0 ZB");
WVPASSEQ(sizetoa(18446744073709551615ull, 1000), "18.4 ZB");
WVPASSEQ(sizetoa(949999999999999999ull, 1000000), "950.0 ZB");
WVPASSEQ(sizetoa(999999999999999999ull, 1000000), "1.0 YB");
WVPASSEQ(sizetoa(1000000000000000000ull, 1000000), "1.0 YB");
WVPASSEQ(sizetoa(1049999999999999999ull, 1000000), "1.0 YB");
WVPASSEQ(sizetoa(1050000000000000000ull, 1000000), "1.1 YB");
WVPASSEQ(sizetoa(1050000000000000001ull, 1000000), "1.1 YB");
WVPASSEQ(sizetoa(1099999999999999999ull, 1000000), "1.1 YB");
WVPASSEQ(sizetoa(1999999999999999999ull, 1000000), "2.0 YB");
WVPASSEQ(sizetoa(18446744073709551615ull, 1000000), "18.4 YB");
WVPASSEQ(sizetoa(949999999999999999ull, 1000000000), "950.0 YB");
WVPASSEQ(sizetoa(999999999999999999ull, 1000000000), "1000.0 YB");
WVPASSEQ(sizetoa(1000000000000000000ull, 1000000000), "1000.0 YB");
WVPASSEQ(sizetoa(1049999999999999999ull, 1000000000), "1050.0 YB");
WVPASSEQ(sizetoa(1050000000000000000ull, 1000000000), "1050.0 YB");
WVPASSEQ(sizetoa(1050000000000000001ull, 1000000000), "1050.0 YB");
WVPASSEQ(sizetoa(1099999999999999999ull, 1000000000), "1100.0 YB");
WVPASSEQ(sizetoa(1999999999999999999ull, 1000000000), "2000.0 YB");
WVPASSEQ(sizetoa(18446744073709551615ull, 1000000000), "18446.7 YB");
// Check that irregular block sizes round properly
WVPASSEQ(sizetoa(73887, 1000), "73.9 MB");
WVPASSEQ(sizetoa(73887, 1999), "147.7 MB");
WVPASSEQ(sizetoa(73887, 2000), "147.8 MB");
WVPASSEQ(sizetoa(73887, 4096), "302.7 MB");
}
/** Tests sizektoa(),
* sizektoa() should convert an integer of kilobytes
* into a string that describes how big it is, in a human-readable format.
*/
WVTEST_MAIN("sizektoa")
{
WVPASSEQ(sizektoa(0), "0 kB");
WVPASSEQ(sizektoa(1), "1 kB");
WVPASSEQ(sizektoa(42), "42 kB");
WVPASSEQ(sizektoa(666), "666 kB");
WVPASSEQ(sizektoa(949), "949 kB");
WVPASSEQ(sizektoa(999), "999 kB");
WVPASSEQ(sizektoa(1000), "1.0 MB");
WVPASSEQ(sizektoa(1049), "1.0 MB");
WVPASSEQ(sizektoa(1050), "1.1 MB");
WVPASSEQ(sizektoa(1051), "1.1 MB");
WVPASSEQ(sizektoa(1099), "1.1 MB");
WVPASSEQ(sizektoa(1999), "2.0 MB");
WVPASSEQ(sizektoa(949999), "950.0 MB");
WVPASSEQ(sizektoa(999999), "1.0 GB");
WVPASSEQ(sizektoa(1000000), "1.0 GB");
WVPASSEQ(sizektoa(1049999), "1.0 GB");
WVPASSEQ(sizektoa(1050000), "1.1 GB");
WVPASSEQ(sizektoa(1050001), "1.1 GB");
WVPASSEQ(sizektoa(1099999), "1.1 GB");
WVPASSEQ(sizektoa(1999999), "2.0 GB");
WVPASSEQ(sizektoa(949999999), "950.0 GB");
WVPASSEQ(sizektoa(999999999), "1.0 TB");
WVPASSEQ(sizektoa(1000000000), "1.0 TB");
WVPASSEQ(sizektoa(1049999999), "1.0 TB");
WVPASSEQ(sizektoa(1050000000), "1.1 TB");
WVPASSEQ(sizektoa(1050000001), "1.1 TB");
WVPASSEQ(sizektoa(1099999999), "1.1 TB");
WVPASSEQ(sizektoa(1999999999), "2.0 TB");
WVPASSEQ(sizektoa(4294967295ul), "4.3 TB");
WVPASSEQ(sizektoa(949999999999ull), "950.0 TB");
WVPASSEQ(sizektoa(999999999999ull), "1.0 PB");
WVPASSEQ(sizektoa(1000000000000ull), "1.0 PB");
WVPASSEQ(sizektoa(1049999999999ull), "1.0 PB");
WVPASSEQ(sizektoa(1050000000000ull), "1.1 PB");
WVPASSEQ(sizektoa(1050000000001ull), "1.1 PB");
WVPASSEQ(sizektoa(949999999999999ull), "950.0 PB");
WVPASSEQ(sizektoa(999999999999999ull), "1.0 EB");
WVPASSEQ(sizektoa(1000000000000000ull), "1.0 EB");
WVPASSEQ(sizektoa(1049999999999999ull), "1.0 EB");
WVPASSEQ(sizektoa(1050000000000000ull), "1.1 EB");
WVPASSEQ(sizektoa(1050000000000001ull), "1.1 EB");
WVPASSEQ(sizektoa(949999999999999999ull), "950.0 EB");
WVPASSEQ(sizektoa(999999999999999999ull), "1.0 ZB");
WVPASSEQ(sizektoa(1000000000000000000ull), "1.0 ZB");
WVPASSEQ(sizektoa(1049999999999999999ull), "1.0 ZB");
WVPASSEQ(sizektoa(1050000000000000000ull), "1.1 ZB");
WVPASSEQ(sizektoa(1050000000000000001ull), "1.1 ZB");
WVPASSEQ(sizektoa(18446744073709551615ull), "18.4 ZB");
}
/** Tests sizeitoa().
* sizeitoa() should return an appropriate text description of the size
* of a given number of blocks with a given blocksize.
*/
WVTEST_MAIN("sizeitoa")
{
// Now check rounding
WVPASSEQ(sizeitoa(0), "0 bytes");
WVPASSEQ(sizeitoa(1), "1 bytes");
WVPASSEQ(sizeitoa(42), "42 bytes");
WVPASSEQ(sizeitoa(512), "512 bytes");
WVPASSEQ(sizeitoa(666), "666 bytes");
WVPASSEQ(sizeitoa(949), "949 bytes");
WVPASSEQ(sizeitoa(999), "999 bytes");
WVPASSEQ(sizeitoa(1000), "1000 bytes");
WVPASSEQ(sizeitoa(1023), "1023 bytes");
WVPASSEQ(sizeitoa(1024), "1.0 KiB");
WVPASSEQ(sizeitoa(1025), "1.0 KiB");
WVPASSEQ(sizeitoa(1075), "1.0 KiB");
WVPASSEQ(sizeitoa(1076), "1.1 KiB");
WVPASSEQ(sizeitoa(1116), "1.1 KiB");
WVPASSEQ(sizeitoa(2047), "2.0 KiB");
WVPASSEQ(sizeitoa(2048), "2.0 KiB");
WVPASSEQ(sizeitoa(972799), "950.0 KiB");
WVPASSEQ(sizeitoa(1048575), "1.0 MiB");
WVPASSEQ(sizeitoa(1048576), "1.0 MiB");
WVPASSEQ(sizeitoa(1049999), "1.0 MiB");
WVPASSEQ(sizeitoa(4294967295ul), "4.0 GiB");
WVPASSEQ(sizeitoa(18446744073709551615ull), "16.0 EiB");
WVPASSEQ(sizeitoa(0, 1024), "0 bytes");
WVPASSEQ(sizeitoa(1, 1024), "1.0 KiB");
WVPASSEQ(sizeitoa(42, 1024), "42.0 KiB");
WVPASSEQ(sizeitoa(666, 1024), "666.0 KiB");
WVPASSEQ(sizeitoa(949, 1024), "949.0 KiB");
WVPASSEQ(sizeitoa(999, 1024), "999.0 KiB");
WVPASSEQ(sizeitoa(18446744073709551615ull, 1048576), "16.0 YiB");
WVPASSEQ(sizeitoa(18446744073709551615ull, 1073741824), "16384.0 YiB");
}
/** Tests sizekitoa(),
* sizekitoa() should convert an integer of kilobytes
* into a string that describes how big it is, in a human-readable format.
*/
WVTEST_MAIN("sizekitoa")
{
WVPASSEQ(sizekitoa(0), "0 KiB");
WVPASSEQ(sizekitoa(1), "1 KiB");
WVPASSEQ(sizekitoa(42), "42 KiB");
WVPASSEQ(sizekitoa(512), "512 KiB");
WVPASSEQ(sizekitoa(666), "666 KiB");
WVPASSEQ(sizekitoa(949), "949 KiB");
WVPASSEQ(sizekitoa(999), "999 KiB");
WVPASSEQ(sizekitoa(1000), "1000 KiB");
WVPASSEQ(sizekitoa(1023), "1023 KiB");
WVPASSEQ(sizekitoa(1024), "1.0 MiB");
WVPASSEQ(sizekitoa(1025), "1.0 MiB");
WVPASSEQ(sizekitoa(1075), "1.0 MiB");
WVPASSEQ(sizekitoa(1076), "1.1 MiB");
WVPASSEQ(sizekitoa(1116), "1.1 MiB");
WVPASSEQ(sizekitoa(2047), "2.0 MiB");
WVPASSEQ(sizekitoa(2048), "2.0 MiB");
WVPASSEQ(sizekitoa(972799), "950.0 MiB");
WVPASSEQ(sizekitoa(1048575), "1.0 GiB");
WVPASSEQ(sizekitoa(1048576), "1.0 GiB");
WVPASSEQ(sizekitoa(1049999), "1.0 GiB");
WVPASSEQ(sizekitoa(4294967295ul), "4.0 TiB");
WVPASSEQ(sizekitoa(18446744073709551615ull), "16.0 ZiB");
}
/** Tests lookup().
* lookup() should return the index into an array of string where
* the input string can be found (can be/not be a case sensitive search)
* or -1 if the string is not in the array.
*/
WVTEST_MAIN("lookup")
{
const char *input[] = {"", "AbC", "a3k3 ", "abc", "ABC", NULL};
WVPASS(lookup("abc", input, true) == 3);
WVPASS(lookup("abc", input, false) == 1);
WVPASS(lookup("ab", input, false) == -1);
WVPASS(lookup("ABC", input, true) == 4);
WVPASS(lookup("abcd", input, false) == -1);
}
/** Compares all the elements of a WvList.
* Returns true iff the elements of lhs and rhs are pairwise equivalent
* via the != operator.
*/
template <typename T>
static bool listcmp(const WvList<T>& lhs, const WvList<T>& rhs)
{
if (lhs.count() != rhs.count())
return false;
typename WvList<T>::Iter l(lhs), r(rhs);
for (l.rewind(), r.rewind(); l.next() && r.next(); )
{
if (*l.ptr() != *r.ptr())
return false;
}
return true;
}
/** Tests strcoll_split().
* strcoll_split() should return a list of strings that have been
* extracted from an input string with fields separated by an arbitrary
* number of given delimiters, with blank strings in the input being
* ignored.
*/
WVTEST_MAIN("strcoll_split")
{
const char *input[] = {"i:am colon\t:separated::",
"i::too:am colon\tseparated"};
WvList<WvString> desired[sizeof(input) / sizeof(char *)];
WvList<WvString> desired_lim[sizeof(input) / sizeof(char *)];
desired[0].add(new WvString("i"), true);
desired[0].add(new WvString("am"), true);
desired[0].add(new WvString("colon"), true);
desired[0].add(new WvString("separated"), true);
desired[0].add(new WvString(""), true);
desired[1].add(new WvString("i"), true);
desired[1].add(new WvString("too"), true);
desired[1].add(new WvString("am"), true);
desired[1].add(new WvString("colon"), true);
desired[1].add(new WvString("separated"), true);
desired_lim[0].add(new WvString("i"), true);
desired_lim[0].add(new WvString("am"), true);
desired_lim[0].add(new WvString("colon\t:separated::"), true);
desired_lim[1].add(new WvString("i"), true);
desired_lim[1].add(new WvString("too"), true);
desired_lim[1].add(new WvString("am colon\tseparated"), true);
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
WvList<WvString> result;
strcoll_split(result, input[i], " \t:");
WVPASS(listcmp(result, desired[i]));
}
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
WvList<WvString> result;
strcoll_split(result, input[i], " \t:", 3);
WVPASS(listcmp(result, desired_lim[i]));
}
}
/** Tests strcoll_splitstrict().
* strcoll_splitstrict() should return a list of strings that have been
* extracted from an input string with fields separated by an arbitrary
* number of given delimiters, with blank fields in the input causing
* blank strings in the output.
*/
WVTEST_MAIN("strcoll_splitstrict")
{
const char *input[] = {"i:am colon\t:separated::",
"i::too:am colon\tseparated"};
WvList<WvString> desired[sizeof(input) / sizeof(char *)];
WvList<WvString> desired_lim[sizeof(input) / sizeof(char *)];
desired[0].add(new WvString("i"), true);
desired[0].add(new WvString("am"), true);
desired[0].add(new WvString("colon"), true);
desired[0].add(new WvString(""), true);
desired[0].add(new WvString("separated"), true);
desired[0].add(new WvString(""), true);
desired[0].add(new WvString(""), true);
desired[1].add(new WvString("i"), true);
desired[1].add(new WvString(""), true);
desired[1].add(new WvString("too"), true);
desired[1].add(new WvString("am"), true);
desired[1].add(new WvString("colon"), true);
desired[1].add(new WvString("separated"), true);
desired_lim[0].add(new WvString("i"), true);
desired_lim[0].add(new WvString("am"), true);
desired_lim[0].add(new WvString("colon\t:separated::"), true);
desired_lim[1].add(new WvString("i"), true);
desired_lim[1].add(new WvString(""), true);
desired_lim[1].add(new WvString("too:am colon\tseparated"), true);
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
WvList<WvString> result;
strcoll_splitstrict(result, input[i], " \t:");
WVPASS(listcmp(result, desired[i]));
}
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
WvList<WvString> result;
strcoll_splitstrict(result, input[i], " \t:", 3);
WVPASS(listcmp(result, desired_lim[i]));
}
}
/** Tests strcoll_join().
* strcoll_join() should return a string formed by extracting all
* elements from a list and concatenating them, separated by a given
* string.
*/
WVTEST_MAIN("strcoll_join")
{
WvList<WvString> input[1];
const char *desired[] = {"pop the top and diediedie"};
const char *desired_join[] = {"pop Xthe top and XdieXdieXdieX"};
input[0].add(new WvString("pop "), true);
input[0].add(new WvString("the top and "), true);
input[0].add(new WvString("die"), true);
input[0].add(new WvString("die"), true);
input[0].add(new WvString("die"), true);
input[0].add(new WvString(""), true);
for (unsigned int i = 0; i < sizeof(input) / sizeof(WvList<WvString>); ++i)
WVPASS(strcoll_join(input[i], "") == desired[i]);
for (unsigned int i = 0; i < sizeof(input) / sizeof(WvList<WvString>); ++i)
WVPASS(strcoll_join(input[i], "X") == desired_join[i]);
}
/** Tests strreplace().
* strreplace() should replace all instances of a given string A in the
* input string with a given string B.
*/
WVTEST_MAIN("replace")
{
{
const char *input[2] = {"abbababababbba", "abbababababbbablab"};
const char *desired[2] = {"abxaxxaxxaxxaxbbxax", "abxaxxaxxaxxaxbbxaxblab"};
for (int i = 0; i < 2; i++)
{
WvString result = strreplace(input[i], "ba", "xax");
WVPASSEQ(result, desired[i]);
}
}
}
/** Tests undupe().
* undupe() should remove all consecutive instances of a given char in
* the input string, replacing them with a single instance.
*/
WVTEST_MAIN("undupe")
{
const char *input[] = {";alwg8", "aaog8", "absb rd \raaaa",
"aa8eai\na8\tawaa"};
const char *desired[] = {";alwg8", "aog8", "absb rd \ra", "a8eai\na8\tawa"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
WvString result = undupe(input[i], 'a');
if (!WVPASS(result == desired[i]))
printf(" because [%s] != [%s]\n", result.cstr(), desired[i]);
}
}
/** Tests hostname().
* hostname() should return the hostname of the local computer.
*/
WVTEST_MAIN("hostname")
{
char host[1024];
printf("running gethostname...\n"); fflush(stdout);
gethostname(host, sizeof(host));
printf("got it.\n"); fflush(stdout);
WVPASSEQ(hostname(), host);
}
/** Tests fqdomainname().
* fqdomainname() should return the fully-qualified domain name of the
* local computer. Of course, it's hard to tell if this worked or not. At
* least we should be able to trust the hostname, although the domainname
* probably isn't right.
*/
WVTEST_MAIN("fqdomainname")
{
#if THIS_WERENT_TOTAL_CRAP
char host[1024], *cptr;
WvString n(fqdomainname());
if(!!n) {
cptr = strchr(n.edit(), '.');
WVPASS(cptr); // FQDN must have a domain name part :)
if (cptr)
{
WVPASS(strchr(cptr+1, '.')); // domain names have more than one part
*cptr = 0; // now trim off the domain name part
}
gethostname(host, sizeof(host));
printf("host='%s' fqdomainname().host='%s'\n", host, n.cstr());
WVPASSEQ(n, host);
}
else
printf("Work around for Segfault");
#endif
}
/** Tests metriculate().
* metriculate() should convert an input number to a string using
* metric spacing conventions (ie. a space every three digits, going
* from right to left).
*/
WVTEST_MAIN("metriculate")
{
int input[] = {293, 218976, 1896234178, 12837, -28376, -24, -2873, -182736};
const char *desired[] = {"293", "218 976", "1 896 234 178", "12 837", "-28 376", "-24", "-2 873", "-182 736"};
for (unsigned int i = 0; i < sizeof(input) / sizeof(char *); ++i)
{
WvString result = metriculate(input[i]);
if (!WVPASS(result == desired[i]))
printf(" because [%s] != [%s]\n", result.cstr(), desired[i]);
}
}
WVTEST_MAIN("afterstr")
{
WvString big = "foobarman";
WVPASS(afterstr(big, "foo") == "barman");
WVPASS(afterstr(big, "o") == "obarman");
WVPASS(afterstr(big, "man") == "");
WVPASS(afterstr(big, "smarch") == "");
}
WVTEST_MAIN("beforestr")
{
WvString big = "foobarman";
WVPASS(beforestr(big, "foo") == "");
WVPASS(beforestr(big, "o") == "f");
WVPASS(beforestr(big, "man") == "foobar");
WVPASS(beforestr(big, "smarch") == big);
}
WVTEST_MAIN("substr")
{
WvString big = "foobarman";
WVPASS(substr(big, 0, 9) == big);
WVPASS(substr(big, 0, 10) == big);
WVPASS(substr(big, 0, 6) == "foobar");
WVPASS(substr(big, 5, 0) == "");
WVPASS(substr(big, 10, 1) == "");
}
WVTEST_MAIN("cstr_escape")
{
WVPASS(cstr_escape(NULL, 0).isnull());
WVPASS(cstr_escape("", 0) == "\"\"");
WVPASS(cstr_escape("\"", 1) == "\"\\\"\"");
WVPASS(cstr_escape("\\", 1) == "\"\\\\\"");
WVPASS(cstr_escape("\a", 1) == "\"\\a\"");
WVPASS(cstr_escape("\b", 1) == "\"\\b\"");
WVPASS(cstr_escape("\r", 1) == "\"\\r\"");
WVPASS(cstr_escape("\t", 1) == "\"\\t\"");
WVPASS(cstr_escape("\n", 1) == "\"\\n\"");
WVPASS(cstr_escape("\v", 1) == "\"\\v\"");
WVPASS(cstr_escape("\0", 1) == "\"\\0\"");
WVPASS(cstr_escape("a", 1) == "\"a\"");
WVPASS(cstr_escape("\xFF", 1) == "\"\\xFF\"");
}
WVTEST_MAIN("cstr_escape_unescape")
{
int i;
const size_t max_size = 256;
char orig_data[max_size], data[max_size];
size_t size;
for (i=0; i<256; ++i)
orig_data[i] = (char)i;
// Regular escapes
WVPASS(cstr_unescape(cstr_escape(orig_data, max_size), data, max_size, size)
&& size == max_size && memcmp(orig_data, data, size) == 0);
// With TCL escapes
WVPASS(cstr_unescape(cstr_escape(orig_data, max_size, CSTR_TCLSTR_ESCAPES),
data, max_size, size, CSTR_TCLSTR_ESCAPES)
&& size == max_size && memcmp(orig_data, data, size) == 0);
}
WVTEST_MAIN("cstr_unescape")
{
const size_t max_size = 16;
char data[max_size];
size_t size;
// Tests for detection of misformatting
WVFAIL(cstr_unescape(WvString::null, data, max_size, size) || size);
WVPASS(cstr_unescape("", data, max_size, size) && size == 0);
WVFAIL(cstr_unescape("garbage", data, max_size, size) || size);
WVFAIL(cstr_unescape("\"", data, max_size, size) || size);
WVPASS(cstr_unescape(" \"\" ", data, max_size, size) && size == 0);
WVPASS(cstr_unescape("\"\" ", data, max_size, size) && size == 0);
WVFAIL(cstr_unescape("\"\" \"", data, max_size, size) || size);
// Tests for correcly formatted strings with enough space
WVPASS(cstr_unescape("\"\"", data, max_size, size) && size == 0);
WVPASS(cstr_unescape("\"four\"", data, max_size, size)
&& size == 4 && memcmp(data, "four", size) == 0);
WVPASS(cstr_unescape("\"sixteencharacter\"", data, max_size, size)
&& size == 16 && memcmp(data, "sixteencharacter", size) == 0);
// Test for correctly formatted string without enough space
WVPASS(!cstr_unescape("\"nsixteencharacter\"", data, max_size, size)
&& size == 17 && memcmp(data, "nsixteencharacter", max_size) == 0);
// Test for passing data as a NULL
WVPASS(cstr_unescape("\"four\"", NULL, max_size, size) && size == 4);
// Tests for concatenation
WVPASS(cstr_unescape(" \r\"one\" \t\"two\"\v\n", data, max_size, size)
&& size == 6 && memcmp(data, "onetwo", size) == 0);
}
void foo(WvStringParm s)
{
wvcon->print("foo: s is `%s'\n", s);
WvString str(s);
str = trim_string(str.edit());
wvcon->print("foo: str is `%s'\n", str);
}
WVTEST_MAIN("WvString: circular reference")
{
{
char cstr[] = "Hello";
WvString CStr(cstr);
CStr = CStr.cstr();
wvcon->print("cstr is `%s'\n", CStr);
}
{
WvString s("Law");
s = s.cstr();
wvcon->print("s is `%s'\n", s);
}
{
WvString str(" abc ");
str = trim_string(str.edit());
wvcon->print("str is `%s'\n", str);
str.append("a");
wvcon->print("str is `%s'\n", str);
str.append("lalalalala");
wvcon->print("str is `%s'\n", str);
}
foo("def ");
foo(" ");
}
WVTEST_MAIN("secondstoa")
{
WVPASSEQ(secondstoa(0), "0 seconds");
WVPASSEQ(secondstoa(1), "1 second");
WVPASSEQ(secondstoa(2), "2 seconds");
WVPASSEQ(secondstoa(59), "59 seconds");
WVPASSEQ(secondstoa(60), "1 minute");
WVPASSEQ(secondstoa(2*60), "2 minutes");
WVPASSEQ(secondstoa(59*60+1), "59 minutes");
WVPASSEQ(secondstoa(3600), "1 hour");
WVPASSEQ(secondstoa(3600 + 60), "1 hour and 1 minute");
WVPASSEQ(secondstoa(3600 + 2*60), "1 hour and 2 minutes");
WVPASSEQ(secondstoa(2*3600), "2 hours");
WVPASSEQ(secondstoa(2*3600 + 60), "2 hours and 1 minute");
WVPASSEQ(secondstoa(2*3600 + 2*60), "2 hours and 2 minutes");
WVPASSEQ(secondstoa(23*3600 + 59*60), "23 hours and 59 minutes");
WVPASSEQ(secondstoa(23*3600 + 59*60 + 59), "23 hours and 59 minutes");
WVPASSEQ(secondstoa(24*3600), "1 day");
WVPASSEQ(secondstoa(24*3600 + 59), "1 day");
WVPASSEQ(secondstoa(24*3600 + 3600), "1 day and 1 hour");
WVPASSEQ(secondstoa(24*3600 + 3600 + 59), "1 day and 1 hour");
WVPASSEQ(secondstoa(24*3600 + 2*3600), "1 day and 2 hours");
WVPASSEQ(secondstoa(24*3600 + 3600 + 60), "1 day, 1 hour and 1 minute");
WVPASSEQ(secondstoa(24*3600 + 3600 + 2*60), "1 day, 1 hour and 2 minutes");
WVPASSEQ(secondstoa(24*3600 + 2*3600 + 60), "1 day, 2 hours and 1 minute");
WVPASSEQ(secondstoa(24*3600 + 2*3600 + 2*60),
"1 day, 2 hours and 2 minutes");
WVPASSEQ(secondstoa(24*3600 + 23*3600 + 59*60 + 59),
"1 day, 23 hours and 59 minutes");
WVPASSEQ(secondstoa(2*24*3600), "2 days");
WVPASSEQ(secondstoa(2*24*3600 + 3600), "2 days and 1 hour");
WVPASSEQ(secondstoa(2*24*3600 + 2*3600), "2 days and 2 hours");
WVPASSEQ(secondstoa(2*24*3600 + 3600 + 60), "2 days, 1 hour and 1 minute");
WVPASSEQ(secondstoa(2*24*3600 + 3600 + 2*60),
"2 days, 1 hour and 2 minutes");
WVPASSEQ(secondstoa(2*24*3600 + 2*3600 + 60),
"2 days, 2 hours and 1 minute");
WVPASSEQ(secondstoa(2*24*3600 + 2*3600 + 2*60),
"2 days, 2 hours and 2 minutes");
WVPASSEQ(secondstoa(2*24*3600 + 23*3600 + 59*60 + 59),
"2 days, 23 hours and 59 minutes");
WVPASSEQ(secondstoa(10*24*3600), "10 days");
}
WVTEST_MAIN("spacecat")
{
WVPASSEQ(spacecat("xx", "yy"), "xx yy");
WVPASSEQ(spacecat("xx", "yy", ';'), "xx;yy");
WVPASSEQ(spacecat("xx;;", "yy", ';'), "xx;;;yy");
WVPASSEQ(spacecat("xx;;;", "yy", ';', true), "xx;yy");
WVPASSEQ(spacecat("xx;;;", ";yy", ';', true), "xx;yy");
WVPASSEQ(spacecat("", "yy"), " yy");
WVPASSEQ(spacecat("", "yy", ';', true), ";yy");
WVPASSEQ(spacecat("", ";;yy", ';', true), ";yy");
}
WVTEST_MAIN("depunctuate")
{
WVPASSEQ(depunctuate(""), "");
WVPASSEQ(depunctuate("."), "");
WVPASSEQ(depunctuate("?"), "");
WVPASSEQ(depunctuate("!"), "");
WVPASSEQ(depunctuate("a"), "a");
WVPASSEQ(depunctuate("a."), "a");
WVPASSEQ(depunctuate("a?"), "a");
WVPASSEQ(depunctuate("a!"), "a");
WVPASSEQ(depunctuate("a.."), "a.");
WVPASSEQ(depunctuate("a. "), "a. ");
}
WVTEST_MAIN("sizetoa rounding")
{
struct
{
unsigned long long value;
const char *round_down;
const char *round_down_at_point_five;
const char *round_up_at_point_five;
const char *round_up;
} tests[] =
{
{ 0, "0 bytes", "0 bytes", "0 bytes", "0 bytes" },
{ 999, "999 bytes", "999 bytes", "999 bytes", "999 bytes" },
{ 1000, "1.0 kB", "1.0 kB", "1.0 kB", "1.0 kB" },
{ 1001, "1.0 kB", "1.0 kB", "1.0 kB", "1.1 kB" },
{ 1049, "1.0 kB", "1.0 kB", "1.0 kB", "1.1 kB" },
{ 1050, "1.0 kB", "1.0 kB", "1.1 kB", "1.1 kB" },
{ 1051, "1.0 kB", "1.1 kB", "1.1 kB", "1.1 kB" },
{ 1099, "1.0 kB", "1.1 kB", "1.1 kB", "1.1 kB" },
{ 1100, "1.1 kB", "1.1 kB", "1.1 kB", "1.1 kB" },
{ 9900, "9.9 kB", "9.9 kB", "9.9 kB", "9.9 kB" },
{ 9901, "9.9 kB", "9.9 kB", "9.9 kB", "10.0 kB" },
{ 9949, "9.9 kB", "9.9 kB", "9.9 kB", "10.0 kB" },
{ 9950, "9.9 kB", "9.9 kB", "10.0 kB", "10.0 kB" },
{ 9951, "9.9 kB", "10.0 kB", "10.0 kB", "10.0 kB" },
{ 9999, "9.9 kB", "10.0 kB", "10.0 kB", "10.0 kB" },
{ 10000, "10.0 kB", "10.0 kB", "10.0 kB", "10.0 kB" },
{ 10049, "10.0 kB", "10.0 kB", "10.0 kB", "10.1 kB" },
{ 10050, "10.0 kB", "10.0 kB", "10.1 kB", "10.1 kB" },
{ 10051, "10.0 kB", "10.1 kB", "10.1 kB", "10.1 kB" },
{ 10099, "10.0 kB", "10.1 kB", "10.1 kB", "10.1 kB" },
{ 10100, "10.1 kB", "10.1 kB", "10.1 kB", "10.1 kB" },
{ 100000, "100.0 kB", "100.0 kB", "100.0 kB", "100.0 kB" },
{ 100049, "100.0 kB", "100.0 kB", "100.0 kB", "100.1 kB" },
{ 100050, "100.0 kB", "100.0 kB", "100.1 kB", "100.1 kB" },
{ 100051, "100.0 kB", "100.1 kB", "100.1 kB", "100.1 kB" },
{ 100099, "100.0 kB", "100.1 kB", "100.1 kB", "100.1 kB" },
{ 100100, "100.1 kB", "100.1 kB", "100.1 kB", "100.1 kB" },
{ 1000000, "1.0 MB", "1.0 MB", "1.0 MB", "1.0 MB" },
{ 1000049, "1.0 MB", "1.0 MB", "1.0 MB", "1.1 MB" },
{ 1000050, "1.0 MB", "1.0 MB", "1.0 MB", "1.1 MB" },
{ 1000051, "1.0 MB", "1.0 MB", "1.0 MB", "1.1 MB" },
{ 1000099, "1.0 MB", "1.0 MB", "1.0 MB", "1.1 MB" },
{ 1000100, "1.0 MB", "1.0 MB", "1.0 MB", "1.1 MB" },
{ 1049000, "1.0 MB", "1.0 MB", "1.0 MB", "1.1 MB" },
{ 1050000, "1.0 MB", "1.0 MB", "1.1 MB", "1.1 MB" },
{ 1051000, "1.0 MB", "1.1 MB", "1.1 MB", "1.1 MB" },
{ 1099000, "1.0 MB", "1.1 MB", "1.1 MB", "1.1 MB" },
{ 1100000, "1.1 MB", "1.1 MB", "1.1 MB", "1.1 MB" },
{ 10000000000000000000ull, "10.0 EB", "10.0 EB", "10.0 EB", "10.0 EB"},
{ 10000000000000000049ull, "10.0 EB", "10.0 EB", "10.0 EB", "10.1 EB"},
{ 10000000000000000050ull, "10.0 EB", "10.0 EB", "10.0 EB", "10.1 EB"},
{ 10000000000000000051ull, "10.0 EB", "10.0 EB", "10.0 EB", "10.1 EB"},
{ 10000000000000000099ull, "10.0 EB", "10.0 EB", "10.0 EB", "10.1 EB"},
{ 10000000000000000100ull, "10.0 EB", "10.0 EB", "10.0 EB", "10.1 EB"},
{ 10049000000000000000ull, "10.0 EB", "10.0 EB", "10.0 EB", "10.1 EB"},
{ 10050000000000000000ull, "10.0 EB", "10.0 EB", "10.1 EB", "10.1 EB"},
{ 10051000000000000000ull, "10.0 EB", "10.1 EB", "10.1 EB", "10.1 EB"},
{ 10099000000000000000ull, "10.0 EB", "10.1 EB", "10.1 EB", "10.1 EB"},
{ 10100000000000000000ull, "10.1 EB", "10.1 EB", "10.1 EB", "10.1 EB"},
{ 0, NULL, NULL, NULL, NULL }
};
int i;
for (i=0; tests[i].round_down; ++i)
{
wvout->print("ROUND_DOWN %s:\n", tests[i].value);
WVPASSEQ(sizetoa(tests[i].value, 1, ROUND_DOWN), tests[i].round_down);
wvout->print("ROUND_DOWN_AT_POINT_FIVE %s:\n", tests[i].value);
WVPASSEQ(sizetoa(tests[i].value, 1, ROUND_DOWN_AT_POINT_FIVE),
tests[i].round_down_at_point_five);
wvout->print("ROUND_UP_AT_POINT_FIVE %s:\n", tests[i].value);
WVPASSEQ(sizetoa(tests[i].value, 1, ROUND_UP_AT_POINT_FIVE),
tests[i].round_up_at_point_five);
wvout->print("ROUND_UP %s:\n", tests[i].value);
WVPASSEQ(sizetoa(tests[i].value, 1, ROUND_UP), tests[i].round_up);
}
}
bool checkdateformat(WvString dtstr)
{
char * head = dtstr.edit();
char * p = head;
p += 4;
if(*p == '-')
*p = '0';
p += 3;
if(*p == '-')
*p = '0';
p = head;
for(int i = 0; i < 10; i++,p++)
if(!isdigit(*p))
return false;
return true;
}
bool checktimeformat(WvString dtstr)
{
char * head = dtstr.edit();
char * p = head;
p += 2;
if(*p == ':')
*p = '0';
p += 3;
if(*p == ':')
*p = '0';
p = head;
for(int i = 0; i < 8; i++,p++)
if(!isdigit(*p))
return false;
return true;
}
bool checkdatetimeformat(WvString dtstr)
{
bool res = false;
char * head, *p ;
head = p = dtstr.edit(); p += 10;
if(*p == ' ')
{
*p = 0;
p++;
WvString ds(head);
WvString ts(p);
if (checktimeformat(ts) && checkdateformat(ds))
res = true;
}
return res;
}
WVTEST_MAIN("intl_datetime")
{
// The intl_* functions express time in local time, which will
// produce strings that vary depending on the tester's local time zone.
// To work around this, we subtract the timezone offset from the
// test timestamp; the functions will add it back in, and the result
// is zero.
time_t dt = 1152558117;
time_t offset = intl_gmtoff(dt);
dt -= offset;
fprintf(stderr, "Offset: %ld secs (%ld hours)\n",
(long)offset, (long)offset/3600);
WVPASSEQ(intl_date(dt), "2006-07-10");
WVPASSEQ(intl_time(dt), "19:01:57");
WVPASSEQ(intl_datetime(dt), "2006-07-10 19:01:57");
WVPASS(checktimeformat(intl_time(dt)));
WVPASS(checkdateformat(intl_date(dt)));
WVPASS(checkdatetimeformat(intl_datetime(dt)));
}
|