logo

utils-std

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

t_truncation.c (2027B)


  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 "../libutils/truncation.h"
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <stdio.h> // printf
  9. const char *argv0 = "test-lib/truncation";
  10. int counter = 0;
  11. int err = 0;
  12. static void
  13. t_parse_size(const char *str, off_t size, enum operation_e op)
  14. {
  15. int id = ++counter;
  16. struct truncation tr;
  17. int ret = parse_size(str, &tr);
  18. if(ret == 0 && tr.size == size && tr.op == op)
  19. {
  20. printf("ok %d - parse_size(\"%s\", _) -> {.size = %jd, .op = %d}\n",
  21. id,
  22. str,
  23. (intmax_t)tr.size,
  24. tr.op);
  25. return;
  26. }
  27. err = 1;
  28. printf("not ok %d - parse_size(\"%s\", _) -> {.size = %jd, .op = %d}\n",
  29. id,
  30. str,
  31. (intmax_t)tr.size,
  32. tr.op);
  33. if(tr.size != size || tr.op != op)
  34. printf("# Expected: {.size = %jd, .op = %d}\n", (intmax_t)size, op);
  35. if(ret != 0) printf("# Exit status: %d\n", ret);
  36. }
  37. static void
  38. set(void)
  39. {
  40. t_parse_size("0", 0L, OP_SET);
  41. t_parse_size("666", 666L, OP_SET);
  42. t_parse_size("666M", 666 * 1024 * 1024L, OP_SET);
  43. t_parse_size("666MB", 666 * 1000 * 1000L, OP_SET);
  44. t_parse_size("666MiB", 666 * 1024 * 1024L, OP_SET);
  45. }
  46. static void
  47. inc(void)
  48. {
  49. t_parse_size("+0", 0L, OP_INC);
  50. t_parse_size("+666", 666L, OP_INC);
  51. t_parse_size("+666M", 666 * 1024 * 1024L, OP_INC);
  52. t_parse_size("+666MB", 666 * 1000 * 1000L, OP_INC);
  53. t_parse_size("+666MiB", 666 * 1024 * 1024L, OP_INC);
  54. }
  55. static void
  56. dec(void)
  57. {
  58. t_parse_size("-0", 0L, OP_DEC);
  59. t_parse_size("-666", 666L, OP_DEC);
  60. t_parse_size("-666M", 666 * 1024 * 1024L, OP_DEC);
  61. t_parse_size("-666MB", 666 * 1000 * 1000L, OP_DEC);
  62. t_parse_size("-666MiB", 666 * 1024 * 1024L, OP_DEC);
  63. }
  64. int
  65. main(void)
  66. {
  67. int plan = 15;
  68. printf("1..%d\n", plan);
  69. // needs standard error capture
  70. //t_parse_size("", 0, OP_SET);
  71. set();
  72. inc();
  73. dec();
  74. assert(counter == plan);
  75. return err;
  76. }