# Copyright (c) 2017 Fred Schlechter 
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

atf_test_case empty_file 
empty_file_head() {
	atf_set "descr" "Test head(1)'s handling of an empty file"
}
empty_file_body() {
	touch infile expectfile
	head infile > outfile
	head < infile > outpipe
	atf_check cmp expectfile outfile
	atf_check cmp expectfile outpipe
}

atf_test_case default_no_options
default_no_options_head() {
	atf_set "descr" "Test head(1)'s default mode"
}
default_no_options_body() {
	#head(1) is supposed to default to 10 lines of output. Verify that it does that.
	jot -b test 10 > expectfile
	jot -b test 100 > infile
	head infile > outfile
	atf_check -e empty cmp expectfile outfile
}

atf_test_case line_count
line_count_head() {
	atf_set "descr" "Test head(1)'s -n option" 
}
line_count_body() {
	jot -b test 100 > outfile
	head -n 50 outfile > expectfile
	atf_check -o inline:"      50 expectfile\n" wc -l expectfile
}

atf_test_case byte_count
byte_count_head() {
	atf_set "descr" "Test head(1)'s -c option"
}
byte_count_body() {
	jot -b test 100 > outfile
	head -c 50 outfile > expectfile
	atf_check -o inline:"      50 expectfile\n" wc -c expectfile
}

atf_test_case sparse_file_text_at_beginning
sparse_file_text_at_beginning_head() {
	atf_set "descr" "Test head(1)'s handling of a sparse file with text at the beginning of the file"
}
sparse_file_text_at_beginning_body () {
	jot -b test 10 > outfile
	truncate -s +1K outfile
	head -c 512 outfile > expectfile
	atf_check -o inline:"     512 expectfile\n" wc -c expectfile
}

atf_test_case sparse_file_text_at_end
sparse_file_text_at_end_head() {
	atf_set "descr" "Test head(1)'s handling of a sparse file with text at the end of the file"
}
sparse_file_text_at_end_body () {
	truncate -s +1K infile 
	echo test >> infile 
	head -c 4096 < infile > outpipe
	atf_check cmp infile outpipe 
}

atf_test_case missing_line_count
missing_line_count_head() {
	atf_set "descr" "Test head(1)'s handling of a missing line count arg"
}
missing_line_count_body () {
	jot -b test 100 > outfile
	atf_check -s not-exit:0 -e not-empty head -n outfile 
}

atf_test_case invalid_line_count
invalid_line_count_head() {
	atf_set "descr" "Test head(1)'s handling of an invalid line count arg"
}
invalid_line_count_body () {
	jot -b test 100 > outfile
	atf_check -s not-exit:0 -e not-empty head -n -10 outfile 
}

atf_test_case read_from_stdin
read_from_stdin_head() {
	atf_set "descr" "Test head(1)'s reading of stdin"
}
read_from_stdin_body() {
	#head(1) defaults to head -n 10 if no args are given.
	jot -b test 10 > outfile
	jot -b test 20 | head > expectfile
	atf_check cmp outfile expectfile 
}

atf_test_case silent_header
silent_header_head() {
	atf_set "descr" "Test head(1)'s silent header feature"
}
silent_header_body() {
	#head(1) defaults to head -n 10 if no args are given.
	jot 11 1 11 > file1
	jot 11 2 12 > file2
	jot 10 1 10 > expectfile
	jot 10 2 11 >> expectfile
	head -q file1 file2 > outfile
	atf_check cmp outfile expectfile
}

atf_test_case verbose_header
verbose_header_head() {
	atf_set "descr" "Test head(1)'s verbose header feature"
}
verbose_header_body() {
	#head(1) defaults to head -n 10 if no args are given.
	jot -b test 10 > file1
	echo '==> file1 <==' > expectfile
	cat file1 >> expectfile
	head -v file1 > outfile
	atf_check cmp outfile expectfile
}

atf_test_case si_number
si_number_head() {
	atf_set "descr" "Test head(1)'s SI number feature"
}
si_number_body() {
	jot -b aaaaaaa 129 > file1
	jot -b aaaaaaa 128 > expectfile
	head -c 1k file1 > outfile
	atf_check cmp outfile expectfile
	jot 1025 1 1025 > file1
	jot 1024 1 1024 > expectfile
	head -n 1k file1 > outfile
	atf_check cmp outfile expectfile
}

atf_init_test_cases() {
	atf_add_test_case empty_file 
	atf_add_test_case default_no_options
	atf_add_test_case line_count
	atf_add_test_case byte_count
	atf_add_test_case sparse_file_text_at_beginning
	atf_add_test_case sparse_file_text_at_end
	atf_add_test_case missing_line_count
	atf_add_test_case invalid_line_count
	atf_add_test_case read_from_stdin
	atf_add_test_case silent_header
	atf_add_test_case verbose_header
	atf_add_test_case si_number
}
