#!/bin/sh
#
#  check-utils - check that zzuf properly fuzzes common utilities
#  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
#                All Rights Reserved
#
#  This program is free software. It comes without any warranty, to
#  the extent permitted by applicable law. You can redistribute it
#  and/or modify it under the terms of the Do What The Fuck You Want
#  To Public License, Version 2, as published by Sam Hocevar. See
#  http://sam.zoy.org/wtfpl/COPYING for more details.
#

. "$(dirname "$0")/functions.inc"

HAVE_GETLINE=$("$ZZCAT" -l | grep >/dev/null 2>&1 '^  getline(' && echo "y")
HAVE_GETDELIM=$("$ZZCAT" -l | grep >/dev/null 2>&1 '^  getdelim(' && echo "y")
HAVE_GETC_UNLOCKED=$("$ZZCAT" -l | grep >/dev/null 2>&1 '^  getc_unlocked(' && echo "y")
HAVE_FGETC_UNLOCKED=$("$ZZCAT" -l | grep >/dev/null 2>&1 '^  fgetc_unlocked(' && echo "y")

checkutils()
{
    r=$1
    for type in 00 ff text random; do
        file="$DIR/file-$type"
        ZZOPTS="-s $seed -r $r"
        case $file in
          *text*) ZZOPTS="$ZZOPTS -P '\\n' -R '\\000'" ;;
        esac
        echo "*** file $file, ratio $r ***"
        REFMD5=""
        if [ $r = 0.0 -a $type = 00 ]; then
            check="bb7df04e1b0a2570657527a7e108ae23"
            echo "*** should be $check ***"
            check "$ZZOPTS" "< $file" "zzuf" "$check"
        else
            check "$ZZOPTS" "< $file" "zzuf"
        fi
        if [ "$STATIC_CAT" = "" ]; then
            check "$ZZOPTS" "cat $file" "cat"
            check "$ZZOPTS" "-i cat < $file" "|cat"
        fi
        if [ "$STATIC_DD" = "" ]; then
            check "$ZZOPTS" "dd bs=65536 if=$file" "dd(bs=65536)"
            check "$ZZOPTS" "dd bs=1111 if=$file" "dd(bs=1111)"
            check "$ZZOPTS" "dd bs=1024 if=$file" "dd(bs=1024)"
            check "$ZZOPTS" "dd bs=1 if=$file" "dd(bs=1)"
        fi
        case $file in
          *text*)
            # We don't include grep or sed when the input is not text, because
            # they put a newline at the end of their input if it was not there
            # initially. (Linux sed doesn't, but OS X sed does.)
            check "$ZZOPTS" "head -n 9999 $file" "head -n 9999"
            check "$ZZOPTS" "${TAILN}9999 $file" "${TAILN}9999"
            check "$ZZOPTS" "${TAILP}1 $file" "${TAILP}1"
            if grep -a '' /dev/null >/dev/null 2>&1; then
                check "$ZZOPTS" "grep -a '' $file" "grep -a ''"
            fi
            check "$ZZOPTS" "sed -e n $file" "sed -e n"
            #check "$ZZOPTS" "cut -b1- $file" "cut -b1-"
            check "$ZZOPTS" "-i head -n 9999 < $file" "|head -n 9999"
            check "$ZZOPTS" "-i ${TAILN}9999 < $file" "|${TAILN}9999"
            check "$ZZOPTS" "-i ${TAILP}1 < $file" "|${TAILP}1"
            if grep -a '' /dev/null >/dev/null 2>&1; then
                check "$ZZOPTS" "-i grep -a '' < $file" "|grep -a ''"
            fi
            check "$ZZOPTS" "-i sed -e n < $file" "|sed -e n"
            #check "$ZZOPTS" "-i cut -b1- < $file" "|cut -b1-"
            ;;
        esac
        # Regression tests for stuff that used to break
        check "$ZZOPTS" "$ZZCAT -x \"fread(1,33000) fseek(1,SEEK_SET) fread(1,1) fseek(4093,SEEK_CUR) fread(1,1) fseek(1,SEEK_CUR) fread(1,1)\" $file" \
              "eglibc (2.9-21) bug regression"
        if [ "$HAVE_GET_UNLOCKED $HAVE_GETLINE" = "y y" ]; then
          check "$ZZOPTS" "$ZZCAT -x \"repeat(33000,getc_unlocked(),feof(10),ungetc(),getline()))\" $file" \
                "sed getc_unlocked() bug regression"
        fi
        # Misc tests
        for n in \
          "fread(1,33000)" \
          "repeat(-1,fread(1,1000),feof(1))" \
          "repeat(-1,fread(1,33),feof(1))" \
          "getc() fread(1,33000)" \
          "fgetc() fread(1,33000)" \
          "getc() ungetc() fread(1,33000)" \
          "fgetc() ungetc() fread(1,33000)" \
          "repeat(-1,getc(),feof(10))" \
          "repeat(-1,fgetc(),feof(10))" \
          "repeat(8000,getc()) fread(1,33000)" \
          "repeat(8000,fgetc()) fread(1,33000)" \
          "fread(1,100) fseek(50,SEEK_SET) fread(1,33000)" \
          "fread(1,100) rewind() fseek(50,SEEK_CUR) fread(1,33000)" \
          "fread(1,33000) rewind() repeat(-1,fseek(1,SEEK_CUR),fread(1,1),feof(1))" \
          "fread(1,33000) rewind() repeat(-1,fseek(2,SEEK_CUR),fread(1,2),feof(1))" \
          "fread(1,33000) rewind() repeat(-1,fseek(3,SEEK_CUR),fread(1,3),feof(1))" \
          "fread(1,33000) rewind() repeat(-1,fseek(4,SEEK_CUR),fread(1,4),feof(1))" \
          "fread(1,33000) fseek(1000,SEEK_CUR) repeat(-1,fread(1,2),feof(10))" \
          "fread(1,33000) fseek(1000,SEEK_CUR) repeat(-1,getc(),feof(10))" \
          "fread(1,33000) fseek(1000,SEEK_CUR) repeat(-1,fgetc(),feof(10))" \
          ; do
            check "$ZZOPTS" "$ZZCAT -x \"$n\" $file" "$n"
        done
        # Platform-specific tests
        if [ "$HAVE_GETLINE" = "y" ]; then
            for n in \
              "repeat(10,getline()) fread(1,33000)" \
              "repeat(-1,getc(),feof(10),ungetc(),getline())" \
              "repeat(-1,fgetc(),feof(10),ungetc(),getline())" \
              ; do
                check "$ZZOPTS" "$ZZCAT -x \"$n\" $file" "$n"
            done
        fi
        if [ "$HAVE_GETDELIM" = "y" ]; then
            for n in \
              "repeat(10,getdelim(0)) fread(1,33000)" \
              "repeat(10,getdelim(' ')) fread(1,33000)" \
              ; do
                check "$ZZOPTS" "$ZZCAT -x \"$n\" $file" "$n"
            done
        fi
        if [ "$HAVE_GETC_UNLOCKED" = "y" ]; then
            for n in \
              "getc_unlocked() fread(1,33000)" \
              "getc_unlocked() ungetc() fread(1,33000)" \
              "repeat(-1,getc_unlocked(),feof(10))" \
              "repeat(8000,getc_unlocked()) fread(1,33000)" \
              "fread(1,33000) fseek(1000,SEEK_CUR) repeat(-1,getc_unlocked(),feof(10))" \
              ; do
                check "$ZZOPTS" "$ZZCAT -x \"$n\" $file" "$n"
            done
        fi
        if [ "$HAVE_FGETC_UNLOCKED" = "y" ]; then
            for n in \
              "fgetc_unlocked() fread(1,33000)" \
              "fgetc_unlocked() ungetc() fread(1,33000)" \
              "repeat(-1,fgetc_unlocked(),feof(10))" \
              "repeat(8000,fgetc_unlocked()) fread(1,33000)" \
              ; do
                check "$ZZOPTS" "$ZZCAT -x \"$n\" $file" "$n"
            done
        fi
    done
}

check()
{
    ZZOPTS="$1"
    CMD="$2"
    ALIAS="$3"
    CHECK="$4"
    printf " $(echo "$ALIAS .................................." | cut -b1-38) "
    MD5="$(eval "$ZZUF -m $ZZOPTS $CMD" 2>/dev/null | cut -f2 -d' ')"
    if [ -n "$CHECK" ]; then
        REFMD5="$CHECK"
    fi
    if [ -z "$REFMD5" ]; then
        REFMD5="$MD5"
        echo "$MD5"
    elif [ "$MD5" != "$REFMD5" ]; then
        fail_test "$MD5 FAILED"
    else
        pass_test 'ok'
    fi
}

start_test "zzuf utils test suite"

checkutils 0.0
checkutils 0.001
checkutils 1.0

stop_test

