teststring.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2018-2023 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "libks/ks.h"
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "tap.h"
  27. struct human_test_entry_s {
  28. ks_size_t size;
  29. int max_precision;
  30. const char *value;
  31. };
  32. static void test_human_size()
  33. {
  34. static const struct human_test_entry_s entries[] = {
  35. { 1024ull, 1, "1.0kB" },
  36. { 1024ull * 1024, 1, "1.0MB" },
  37. { 1024ull * 1024 * 1024, 1, "1.0GB" },
  38. { 1024ull * 1024 * 1024 * 1024, 1, "1.0TB" },
  39. { 1024ull * 1024 * 1024 * 1024 * 1024, 1, "1.0PB" },
  40. { 1024ull * 1024 * 1024 * 1024 * 1024 * 1024, 1, "1.0EB" },
  41. // { 1024ull * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, 1, "1.0ZB" },
  42. // { 1024ull * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, 1, "1.0YB" },
  43. { 1024ull, 0, "1kB" },
  44. { 1024ull * 1024, 0, "1MB" },
  45. { 1024ull * 1024 * 1024, 0, "1GB" },
  46. { 1024ull * 1024 * 1024 * 1024, 0, "1TB" },
  47. { 1024ull * 1024 * 1024 * 1024 * 1024, 0, "1PB" },
  48. // { 1024ull * 1024 * 1024 * 1024 * 1024 * 1024, 0, "1EB" },
  49. // { 1024ull * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, 0, "1ZB" },
  50. // { 1024ull * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, 0, "1YB" },
  51. {0}
  52. };
  53. for (int32_t index = 0; entries[index].size != 0; index++) {
  54. char workspace[1024];
  55. ks_human_readable_size(entries[index].size,
  56. entries[index].max_precision, sizeof(workspace), workspace);
  57. printf("Checking if: %s == %s\n", workspace, entries[index].value);
  58. ok(!strcmp(workspace, entries[index].value));
  59. }
  60. }
  61. int main(int argc, char **argv)
  62. {
  63. ks_init();
  64. test_human_size();
  65. }