Files

61 lines
1.8 KiB
Bash
Executable File

#!/bin/sh
# tests/test_cmp.sh - Tests for cmp applet
_test_name="cmp"
. "$(dirname "$0")/harness.sh"
_srcdir="$(cd "$(dirname "$0")/../src/cmp/tests" && pwd)"
setup_tmpdir
CMP="$VX cmp"
echo "Running cmp tests..."
# --- basic: identical files ---
echo "0123456789abcdef" > a
cp a a_copy
check_exit "basic: identical files exit 0" 0 $CMP a a_copy
# --- basic: different files ---
echo "0123456789abcdeg" > b
check_exit "basic: different files exit 1" 1 $CMP a b
# --- stdin comparison ---
check_exit "special: cmp a - (stdin identical)" 0 sh -c "$CMP a - < a"
check_exit "special: cmp - a (stdin identical)" 0 sh -c "$CMP - a < a"
check_exit "special: cmp a - (stdin different)" 1 sh -c "$CMP a - < b"
check_exit "special: cmp - a (stdin different)" 1 sh -c "$CMP - a < b"
# --- silent mode ---
check_exit "silent: -s identical" 0 $CMP -s a a_copy
check_exit "silent: -s different" 1 $CMP -s a b
# --- byte offset skip ---
echo -n '1234567890' > skip_a
echo -n 'abc567890' > skip_b
check_exit "skip: -i 4:3 matches" 0 $CMP -s -i 4:3 skip_a skip_b
# --- limit (-n) ---
echo -n "aaaabbbb" > lim_a
echo -n "aaaaxxxx" > lim_b
check_exit "limit: -n 4 identical" 0 $CMP -s -n 4 lim_a lim_b
check_exit "limit: -n 5 different" 1 $CMP -s -n 5 lim_a lim_b
check_exit "limit: full different" 1 $CMP -s lim_a lim_b
# --- -b flag (print differing bytes) ---
echo -n "abcd" > a
echo -n "abdd" > b
_got="$($CMP -b a b 2>/dev/null)" || true
_exp="$(cat "$_srcdir/b_flag.out")"
check_eq "$_got" "$_exp" "bflag: -b output matches"
# --- -bl flags ---
_got="$($CMP -bl a b 2>/dev/null)" || true
_exp="$(cat "$_srcdir/bl_flag.out")"
check_eq "$_got" "$_exp" "blflag: -bl output matches"
# --- nonexistent file ---
$CMP a NOFILE >/dev/null 2>&1
_rc=$?
check_eq "$_rc" "2" "error: nonexistent file exits 2"
summary