logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

t_humanize.c (1909B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #include "../lib/humanize.h"
  6. #include <assert.h>
  7. #include <stdio.h> // printf
  8. const char *argv0 = "t_humanize";
  9. int counter = 0;
  10. int err = 0;
  11. static void
  12. t_dtosi(double in, bool iec, double out, unsigned exponent)
  13. {
  14. int id = ++counter;
  15. struct si_scale ret = dtosi(in, iec);
  16. if(ret.number == out && ret.exponent == exponent)
  17. {
  18. printf("ok %d - dtosi(%f, %d) => {.number = %g, .exponent = %d}\n",
  19. id,
  20. in,
  21. iec,
  22. ret.number,
  23. ret.exponent);
  24. return;
  25. }
  26. err = 1;
  27. printf("not ok %d - dtosi(%f, %d) => {.number = %g, .exponent = %d}\n",
  28. id,
  29. in,
  30. iec,
  31. ret.number,
  32. ret.exponent);
  33. printf("# Expected: {.number = %g, .exponent = %d}\n", out, exponent);
  34. }
  35. int
  36. main(void)
  37. {
  38. int plan = 18;
  39. printf("1..%d\n", plan);
  40. t_dtosi(0, false, 0, 0);
  41. t_dtosi(0, true, 0, 0);
  42. t_dtosi(1000, false, 1000, 0);
  43. t_dtosi(10000, false, 10, 1);
  44. t_dtosi(-1000, false, -1000, 0);
  45. t_dtosi(-10000, false, -10, 1);
  46. t_dtosi(1024, true, 1024, 0);
  47. t_dtosi(10240, true, 10240 / 1024, 1);
  48. t_dtosi(-1024, true, -1024, 0);
  49. t_dtosi(-10240, true, -10240 / 1024, 1);
  50. #define IEC_QiB 1267650600228229401496703205376.0 /* 1024^10 */
  51. t_dtosi(1024 * IEC_QiB, true, 1024, 10);
  52. t_dtosi(-1024 * IEC_QiB, true, -1024, 10);
  53. t_dtosi(1024 * 1024 * IEC_QiB, true, 1024 * 1024, 10);
  54. t_dtosi(-1024 * 1024 * IEC_QiB, true, -1024 * 1024, 10);
  55. #define SI_Q 1000000000000000000000000000000.0 /* 1000^10 */
  56. t_dtosi(1000 * SI_Q, false, 1000, 10);
  57. t_dtosi(-1000 * SI_Q, false, -1000, 10);
  58. t_dtosi(1000 * 1000 * SI_Q, false, 1000 * 1000, 10);
  59. t_dtosi(-1000 * 1000 * SI_Q, false, -1000 * 1000, 10);
  60. assert(counter == plan);
  61. return err;
  62. }