logo

utils-std

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

truncation.c (1902B)


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