Fix truncate prefix bug and add truncate test suite entry.
authorRob Landley <rob@landley.net>
Tue, 28 Apr 2015 17:18:17 +0000 (12:18 -0500)
committerRob Landley <rob@landley.net>
Tue, 28 Apr 2015 17:18:17 +0000 (12:18 -0500)
tests/truncate.test [new file with mode: 0755]
toys/other/truncate.c

diff --git a/tests/truncate.test b/tests/truncate.test
new file mode 100755 (executable)
index 0000000..ee2b2bb
--- /dev/null
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+SIZE='&& stat -c %s freep'
+testing "truncate 0" "truncate -s 0 freep $SIZE" "0\n" "" ""
+testing "truncate 12345" "truncate -s 12345 freep $SIZE" "12345\n" "" ""
+testing "truncate 1m" "truncate -s 1m freep $SIZE" "1048576\n" "" ""
+testing "truncate is sparse" "truncate -s 1g freep && stat -c %b freep" \
+       "0\n" "" ""
+testing "truncate +" "truncate -s 1k freep && truncate -s +1k freep $SIZE" \
+       "2048\n" "" ""
+testing "truncate -" "truncate -s 4k freep && truncate -s -1k freep $SIZE" \
+       "3072\n" "" ""
+testing "truncate < hit" \
+       "truncate -s 5k freep && truncate -s \<4k freep $SIZE" "4096\n" "" ""
+testing "truncate < miss" \
+       "truncate -s 4k freep && truncate -s \<6k freep $SIZE" "4096\n" "" ""
+testing "truncate > hit" \
+       "truncate -s 3k freep && truncate -s \>4k freep $SIZE" "4096\n" "" ""
+testing "truncate > miss" \
+       "truncate -s 4k freep && truncate -s \>2k freep $SIZE" "4096\n" "" ""
+testing "truncate /" "truncate -s 7k freep && truncate -s /3k freep $SIZE" \
+       "6144\n" "" ""
+testing "truncate %" "truncate -s 7k freep && truncate -s %3k freep $SIZE" \
+       "9216\n" "" ""
index 2b3d479..bfe1f10 100644 (file)
@@ -16,7 +16,7 @@ config TRUNCATE
     -s New size (with optional prefix and suffix)
 
     SIZE prefix: + add, - subtract, < shrink to, > expand to,
-                 / block size rounding down, % block size rounding up
+                 / multiple rounding down, % multiple rounding up
     SIZE suffix: k=1024, m=1024^2, g=1024^3, t=1024^4, p=1024^5, e=1024^6
 */
 
@@ -41,7 +41,8 @@ static void do_truncate(int fd, char *name)
     size = fdlength(fd);
     if (TT.type<2) size += TT.size*(1-(2*TT.type));
     else if (TT.type<4) {
-      if ((TT.type==3) ? size > TT.size : size < TT.size) size = TT.size;
+      if ((TT.type==2) ? (size <= TT.size) : (size >= TT.size)) return;
+      size = TT.size;
     } else {
       size = (size+(TT.type-4)*(TT.size-1))/TT.size;
       size *= TT.size;