1#!/bin/sh 2# 3# Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> 4# 5# Permission to use, copy, modify, and distribute this software for any 6# purpose with or without fee is hereby granted, provided that the above 7# copyright notice and this permission notice appear in all copies. 8# 9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 17unset LC_ALL 18 19: ${CUT=cut} 20 21test_cut() 22{ 23 expected_retval=$1 24 args=`echo "$2"` 25 stdin=$3 26 expected=`echo "$4"` 27 export LC_CTYPE=en_US.UTF-8 28 result=`echo -n "$stdin" | $CUT $args 2>/dev/null` 29 retval=$? 30 if [ "$retval" -ne "${expected_retval}" ]; then 31 echo "echo -n \"$stdin\" | $CUT $args" 32 echo -n "$stdin" | hexdump -C 33 echo "expected return value: \"${expected_retval}\"" 34 echo "actual return value: \"$retval\"" 35 exit 1; 36 fi 37 if [ "$result" != "${expected}" ]; then 38 echo "echo -n \"$stdin\" | $CUT $args" 39 echo -n "$stdin" | hexdump -C 40 echo "expected: \"$expected\"" 41 echo -n "$expected" | hexdump -C 42 echo "result: \"$result\"" 43 echo -n "$result" | hexdump -C 44 exit 1; 45 fi 46 47 if [ -n "$5" ]; then 48 expected=`echo "$5"` 49 fi 50 export LC_CTYPE=C 51 result=`echo -n "$stdin" | $CUT $args 2>/dev/null` 52 if [ "$retval" -ne "${expected_retval}" ]; then 53 echo "echo -n \"$stdin\" | $CUT $args" 54 echo -n "$stdin" | hexdump -C 55 echo "expected return value: \"${expected_retval}\"" 56 echo "actual return value: \"$retval\"" 57 exit 1; 58 fi 59 if [ "$result" != "${expected}" ]; then 60 echo "[C] echo -n \"$stdin\" | $CUT $args" 61 echo -n "$stdin" | hexdump -C 62 echo "expected: \"$expected\"" 63 echo -n "$expected" | hexdump -C 64 echo "result: \"$result\"" 65 echo -n "$result" | hexdump -C 66 exit 1; 67 fi 68} 69 70# single byte characters 71test_cut 0 "-b 4,2" "abcde" "bd" 72test_cut 0 "-b 2-4" "abcde" "bcd" 73test_cut 0 "-b 4-,-2" "abcde" "abde" 74test_cut 0 "-nb 4,2" "abcde" "bd" 75test_cut 0 "-nb 2-4" "abcde" "bcd" 76test_cut 0 "-nb 4-,-2" "abcde" "abde" 77test_cut 0 "-c 4,2" "abcde" "bd" 78test_cut 0 "-c 2-4" "abcde" "bcd" 79test_cut 0 "-c 4-,-2" "abcde" "abde" 80 81# multibyte characters 82test_cut 0 "-b 2-3" "ax\0314\0200b" "x\0314" 83test_cut 0 "-b 1,3" "ax\0314\0200b" "a\0314" 84test_cut 0 "-nb 2-3" "ax\0314\0200b" "x" "x\0314" 85test_cut 0 "-nb 1,3" "ax\0314\0200b" "a" "a\0314" 86test_cut 0 "-nb 2,4" "ax\0314\0200b" "x\0314\0200" "x\0200" 87test_cut 0 "-c 2-3" "ax\0314\0200b" "x\0314\0200" "x\0314" 88test_cut 0 "-c 1,3" "ax\0314\0200b" "a\0314\0200" "a\0314" 89 90# double width multibyte characters 91test_cut 0 "-b -3" "a\0354\0277\0277b" "a\0354\0277" 92test_cut 0 "-nb 4-" "a\0354\0277\0277b" "\0354\0277\0277b" "\0277b" 93test_cut 0 "-c 2" "a\0354\0277\0277b" "\0354\0277\0277" "\0354" 94 95# invalid bytes 96test_cut 0 "-b -2" "a\0377\0277b" "a\0377" 97test_cut 0 "-b 3-" "a\0377\0277b" "\0277b" 98test_cut 0 "-nb 2-5" "\0303\0251\0377\0277\0303\0251" "\0303\0251\0377\0277" \ 99 "\0251\0377\0277\0303" 100test_cut 0 "-c 4,1" "\0303\0251\0377\0277\0303\0250" "\0303\0251\0303\0250" \ 101 "\0303\0277" 102 103# multibyte delimiter 104test_cut 0 "-d \0302\0267 -f 2" "a\0302\0267b\0302\0267c" "b" "\0267b" 105test_cut 0 "-d \0302\0267 -f 3,2" "a\0302\0267b\0302\0267c" "b\0302\0267c" \ 106 "\0267b\0302\0267c" 107 108# invalid list values 109test_cut 1 "-b 2,-,4" 110test_cut 1 "-c 2,--,4" 111test_cut 1 "-f 2,---,4" 112test_cut 1 "-b 0-1" 113test_cut 1 "-c 2147483648" 114test_cut 1 "-f not,a-number" 115 116exit 0 117