logo

utils-extra

Collection of extra tools for Unixes git clone https://anongit.hacktivis.me/git/utils-extra.git/

humanize (3681B)


  1. #!/usr/bin/env atf-sh
  2. # SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. atf_test_case one
  5. one_body() {
  6. atf_check -o "inline:1 \n" ../cmd/humanize 1
  7. atf_check -o "inline:1 \n" ../cmd/humanize -d 1
  8. atf_check -o "inline:1 B\n" ../cmd/humanize -b 1
  9. }
  10. atf_test_case two
  11. two_body() {
  12. atf_check -o "inline:12 \n" ../cmd/humanize 12
  13. atf_check -o "inline:12 \n" ../cmd/humanize -d 12
  14. atf_check -o "inline:12 B\n" ../cmd/humanize -b 12
  15. }
  16. atf_test_case three
  17. three_body() {
  18. atf_check -o "inline:123 \n" ../cmd/humanize 123
  19. atf_check -o "inline:123 \n" ../cmd/humanize -d 123
  20. atf_check -o "inline:123 B\n" ../cmd/humanize -b 123
  21. }
  22. # 1 234
  23. atf_test_case four
  24. four_body() {
  25. atf_check -o "inline:1.234 k\n" ../cmd/humanize 1234
  26. atf_check -o "inline:1.234 k\n" ../cmd/humanize -d 1234
  27. atf_check -o "inline:1.20508 KiB\n" ../cmd/humanize -b 1234
  28. }
  29. # 12 345
  30. atf_test_case five
  31. five_body() {
  32. atf_check -o "inline:12.345 k\n" ../cmd/humanize 12345
  33. atf_check -o "inline:12.345 k\n" ../cmd/humanize -d 12345
  34. atf_check -o "inline:12.0557 KiB\n" ../cmd/humanize -b 12345
  35. }
  36. # 123 456
  37. atf_test_case six
  38. six_body() {
  39. atf_check -o "inline:123.456 k\n" ../cmd/humanize 123456
  40. atf_check -o "inline:123.456 k\n" ../cmd/humanize -d 123456
  41. atf_check -o "inline:120.562 KiB\n" ../cmd/humanize -b 123456
  42. }
  43. # 1234 4567
  44. atf_test_case seven
  45. seven_body() {
  46. atf_check -o "inline:1.23457 M\n" ../cmd/humanize 1234567
  47. atf_check -o "inline:1.23457 M\n" ../cmd/humanize -d 1234567
  48. atf_check -o "inline:1.17737 MiB\n" ../cmd/humanize -b 1234567
  49. }
  50. atf_test_case noarg
  51. noarg_body() {
  52. atf_check -s exit:1 -e 'inline:Usage: humanize [-bdt] number\n' ../cmd/humanize
  53. }
  54. atf_test_case badflag
  55. badflag_body() {
  56. atf_check -s exit:1 -e "inline:humanize: Error: Unrecognised option: '-f'\n"'Usage: humanize [-bdt] number\n' ../cmd/humanize -f
  57. atf_check -s exit:1 -e "inline:humanize: Error: Unrecognised option: '-f'\n"'Usage: humanize [-bdt] number\n' ../cmd/humanize -f 123
  58. }
  59. atf_test_case pfx_limit
  60. pfx_limit_body() {
  61. atf_check -o "inline:1024 RiB\n" ../cmd/humanize -b "$(echo 1024^10 | bc -l)"
  62. atf_check -o "inline:1024 QiB\n" ../cmd/humanize -b "$(echo 1024^11 | bc -l)"
  63. atf_check -o "inline:1048576.000000 QiB\n" ../cmd/humanize -b "$(echo 1024^12 | bc -l)"
  64. atf_check -o "inline:1000 R\n" ../cmd/humanize "$(echo 1000^10 | bc -l)"
  65. atf_check -o "inline:1000 Q\n" ../cmd/humanize "$(echo 1000^11 | bc -l)"
  66. atf_check -o "inline:1000000.000000 Q\n" ../cmd/humanize "$(echo 1000^12 | bc -l)"
  67. }
  68. atf_test_case neg
  69. neg_body() {
  70. atf_check -o "inline:-1000 \n" ../cmd/humanize -- -1000
  71. atf_check -o "inline:-1024 B\n" ../cmd/humanize -b -- -1024
  72. atf_check -o "inline:-1000 k\n" ../cmd/humanize -- -1000000
  73. atf_check -o "inline:-1024 KiB\n" ../cmd/humanize -b -- -1048576
  74. }
  75. atf_test_case duration
  76. duration_body() {
  77. atf_check -o "inline:1s\n" ../cmd/humanize -t 1
  78. atf_check -o "inline:1m \n" ../cmd/humanize -t 60
  79. atf_check -o "inline:1h \n" ../cmd/humanize -t 3600
  80. atf_check -o "inline:1D \n" ../cmd/humanize -t 86400
  81. atf_check -o "inline:1M \n" ../cmd/humanize -t 2629743
  82. atf_check -o "inline:1Y \n" ../cmd/humanize -t 31556926
  83. atf_check -o "inline:1Y 1M 1D 1h 1m 1s\n" ../cmd/humanize -t 34276730
  84. }
  85. atf_init_test_cases() {
  86. cd "$(atf_get_srcdir)" || exit 1
  87. atf_add_test_case noarg
  88. atf_add_test_case badflag
  89. atf_add_test_case one
  90. atf_add_test_case two
  91. atf_add_test_case three
  92. atf_add_test_case four
  93. atf_add_test_case five
  94. atf_add_test_case six
  95. atf_add_test_case seven
  96. atf_add_test_case pfx_limit
  97. atf_add_test_case neg
  98. atf_add_test_case duration
  99. }