Index: etc/mtree/BSD.tests.dist =================================================================== --- etc/mtree/BSD.tests.dist +++ etc/mtree/BSD.tests.dist @@ -714,6 +714,8 @@ regress.multitest.out .. .. + seq + .. soelim .. stat Index: usr.bin/seq/Makefile =================================================================== --- usr.bin/seq/Makefile +++ usr.bin/seq/Makefile @@ -1,8 +1,13 @@ # $NetBSD: Makefile,v 1.3 2009/04/14 22:15:26 lukem Exp $ # $FreeBSD$ +.include + PROG= seq LIBADD= m +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests + .include Index: usr.bin/seq/seq.c =================================================================== --- usr.bin/seq/seq.c +++ usr.bin/seq/seq.c @@ -80,7 +80,11 @@ int equalize = 0; double first = 1.0; double last = 0.0; + char lstr[256]; + double cur; + char cstr[256]; double incr = 0.0; + double step; struct lconv *locale; char *fmt = NULL; const char *sep = "\n"; @@ -93,9 +97,9 @@ decimal_point = locale->decimal_point; /* - * Process options, but handle negative numbers separately - * least they trip up getopt(3). - */ + * Process options, but handle negative numbers separately + * least they trip up getopt(3). + */ while ((optind < argc) && !numeric(argv[optind]) && (c = getopt(argc, argv, "f:hs:t:w")) != -1) { @@ -129,7 +133,8 @@ if (errflg) { fprintf(stderr, - "usage: %s [-w] [-f format] [-s string] [-t string] [first [incr]] last\n", + "usage: %s [-w] [-f format] [-s string] [-t string] " + "[first [incr]] last\n", getprogname()); exit(1); } @@ -138,7 +143,7 @@ if (argc > 1) first = e_atof(argv[0]); - + if (argc > 2) { incr = e_atof(argv[1]); /* Plan 9/GNU don't do zero */ @@ -163,22 +168,27 @@ if (!valid_format(fmt)) errx(1, "invalid format string"); /* - * XXX to be bug for bug compatible with Plan 9 add a + * XXX to be bug for bug compatible with Plan 9 add a * newline if none found at the end of the format string. */ } else fmt = generate_format(first, incr, last, equalize, pad); - if (incr > 0) { - for (; first <= last; first += incr) { - printf(fmt, first); - fputs(sep, stdout); - } - } else { - for (; first >= last; first += incr) { - printf(fmt, first); - fputs(sep, stdout); + (void) snprintf(lstr, sizeof(lstr), fmt, last); + for (step = 0; ; step++) { + cur = first + incr * step; + if (incr > 0 ? cur > last : cur < last) { + /* + * To make sure we didn't just hit rounding + * error, compare string representation using + * the output format. + */ + (void) snprintf(cstr, sizeof(cstr), fmt, cur); + if (strcmp(cstr, lstr) != 0) + break; } + printf(fmt, cur); + fputs(sep, stdout); } if (term != NULL) fputs(term, stdout); Index: usr.bin/seq/tests/Makefile =================================================================== --- /dev/null +++ usr.bin/seq/tests/Makefile @@ -0,0 +1,7 @@ +# $FreeBSD$ + +PACKAGE= tests + +ATF_TESTS_SH= seq_test + +.include Index: usr.bin/seq/tests/Makefile.depend =================================================================== --- /dev/null +++ usr.bin/seq/tests/Makefile.depend @@ -0,0 +1,11 @@ +# $FreeBSD$ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif Index: usr.bin/seq/tests/seq_test.sh =================================================================== --- /dev/null +++ usr.bin/seq/tests/seq_test.sh @@ -0,0 +1,67 @@ +# +# Copyright 2018 Yuri Pankov +# +# 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. +# +# $FreeBSD$ + +atf_test_case generic +generic_head() +{ + atf_set "descr" "Verify generic output" +} +generic_body() +{ + # These are here for sanity checks when fixing fp increment + atf_check -o inline:"1 " seq -s ' ' 1 2 2 + atf_check -o inline:"2 " seq -s ' ' 2 -2 1 +} + +atf_test_case float +float_head() +{ + atf_set "descr" "Verify output with floating point increment" +} +float_body() +{ + # Make sure we use correct decimal point + export LC_ALL=C + + atf_check -o inline:"1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 " \ + seq -s ' ' 1 0.1 2 + atf_check -o inline:"2 1.9 1.8 1.7 1.6 1.5 1.4 1.3 1.2 1.1 1 " \ + seq -s ' ' 2 -0.1 1 + atf_check -o inline:"1.6 1.65 1.7 1.75 1.8 1.85 1.9 1.95 2 " \ + seq -s ' ' 1.6 0.05 2 + atf_check -o inline:"2 1.95 1.9 1.85 1.8 1.75 1.7 1.65 1.6 " \ + seq -s ' ' 2 -0.05 1.6 + atf_check -o inline:"1.1 1.2 " seq -s ' ' 1.1 0.1 1.2 + atf_check -o inline:"1.2 1.1 " seq -s ' ' 1.2 -0.1 1.1 + atf_check -o inline:"1 1.3 1.6 1.9 " seq -s ' ' 1 0.3 2 + atf_check -o inline:"2 1.7 1.4 1.1 " seq -s ' ' 2 -0.3 1 +} + +atf_init_test_cases() +{ + atf_add_test_case generic + atf_add_test_case float +}