Advertisement
devinteske

Some snprintf testing in POSIX sh

Jan 26th, 2016
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.59 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. . /usr/share/bsdconfig/strings.subr || exit
  4.  
  5. N=1
  6. EXIT=${SUCCESS:-0}
  7. HR="--------------------"
  8. VERBOSE=
  9.  
  10. hrprint() { printf "%s #%03u %s\n" "$HR" "$N" "$HR"; }
  11. hr() { [ "$VERBOSE" ] && hrprint; }
  12.  
  13. xt()
  14. {
  15.     local testvalue="$1"
  16.     shift 2 # testvalue ==
  17.     hr
  18.     f_snprintf result "$@"
  19.     if [ "$result" = "$testvalue" ]; then
  20.         [ "$VERBOSE" ] && echo f_snprintf "$@" == "$result"
  21.     else
  22.         [ "$VERBOSE" ] || hrprint
  23.         echo f_snprintf "$@" == "$result [bad; expected $testvalue]"
  24.         EXIT=${FAILURE:-1}
  25.     fi
  26.     N=$(( $N + 1 ))
  27. }
  28.  
  29. : 001 ; xt "" == 0 "abc123"
  30. : 002 ; xt "a" == 1 "abc123"
  31. : 003 ; xt "ab" == 2 "abc123"
  32. : 004 ; xt "abc" == 3 "abc123"
  33. : 005 ; xt "abc1" == 4 "abc123"
  34. : 006 ; xt "abc12" == 5 "abc123"
  35. : 007 ; xt "abc123" == 6 "abc123"
  36. : 008 ; xt "abc123" == 7 "abc123"
  37. : 009 ; xt "" == 9  ""
  38. : 010 ; xt "a" == 1 "abc123def456"
  39. : 011 ; xt "ab" == 2 "abc123def456"
  40. : 012 ; xt "abc" == 3 "abc123def456"
  41. : 013 ; xt "abc1" == 4 "abc123def456"
  42. : 014 ; xt "abc12" == 5 "abc123def456"
  43. : 015 ; xt "abc123" == 6 "abc123def456"
  44. : 016 ; xt "abc123d" == 7 "abc123def456"
  45. : 017 ; xt "abc123de" == 8 "abc123def456"
  46. : 018 ; xt "abc123def" == 9 "abc123def456"
  47. : 019 ; xt "abc123def4" == 10 "abc123def456"
  48. : 020 ; xt "abc123def45" == 11 "abc123def456"
  49. : 021 ; xt "abc123def456" == 12 "abc123def456"
  50. : 022 ; xt "abc123def456" == 13 "abc123def456"
  51. : 023 ; xt "abc123def456" == 1025 "abc123def456"
  52. : 024 ; xt "abc12" == 5 "abc123def456ghi789jkl0./"
  53. : 025 ; xt "abc123d" == 7 "abc123def456ghi789jkl0./"
  54. : 026 ; xt "abc123def" == 9 "abc123def456ghi789jkl0./"
  55. : 027 ; xt "abc123def4" == 10 "abc123def456ghi789jkl0./"
  56. : 028 ; xt "abc123def45" == 11 "abc123def456ghi789jkl0./"
  57. : 029 ; xt "abc123def456" == 12 "abc123def456ghi789jkl0./"
  58. : 030 ; xt "abc123def456g" == 13 "abc123def456ghi789jkl0./"
  59. : 031 ; xt "abc123def456gh" == 14 "abc123def456ghi789jkl0./"
  60. : 032 ; xt "abc123def456ghi" == 15 "abc123def456ghi789jkl0./"
  61. : 033 ; xt "abc123def456ghi7" == 16 "abc123def456ghi789jkl0./"
  62. : 034 ; xt "abc123def456ghi78" == 17 "abc123def456ghi789jkl0./"
  63. : 035 ; xt "abc123def456ghi789" == 18 "abc123def456ghi789jkl0./"
  64. : 036 ; xt "abc123def456ghi789j" == 19 "abc123def456ghi789jkl0./"
  65. : 037 ; xt "abc123def456ghi789jk" == 20 "abc123def456ghi789jkl0./"
  66. : 038 ; xt "abc123def456ghi789jkl" == 21 "abc123def456ghi789jkl0./"
  67. : 039 ; xt "abc123def456ghi789jkl0" == 22 "abc123def456ghi789jkl0./"
  68. : 040 ; xt "abc123def456ghi789jkl0." == 23 "abc123def456ghi789jkl0./"
  69. : 041 ; xt "abc123def456ghi789jkl0./" == 24 "abc123def456ghi789jkl0./"
  70. : 042 ; xt "abc123def456ghi789jkl0./" == 25 "abc123def456ghi789jkl0./"
  71.  
  72. exit $EXIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement