From 7074f952167addd7b7ed9d5743c015d14a4f1023 Mon Sep 17 00:00:00 2001 From: j8takagi Date: Tue, 11 Jan 2011 23:17:46 +0900 Subject: [PATCH] =?utf8?q?=E3=82=B5=E3=83=B3=E3=83=97=E3=83=AB=E3=81=AE?= =?utf8?q?=E4=B8=89=E8=A7=92=E5=BD=A2=E5=88=A4=E5=AE=9A=E3=83=97=E3=83=AD?= =?utf8?q?=E3=82=B0=E3=83=A9=E3=83=A0=E3=81=AE=E4=BB=95=E6=A7=98=E3=82=92?= =?utf8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- sample/triangle/test/05_zero/0.txt | 2 +- sample/triangle/test/06_minus/0.txt | 2 +- sample/triangle/test/08_lines/0.txt | 3 +++ sample/triangle/test/08_lines/cmd | 3 +++ sample/triangle/test/10_lesses/0.txt | 3 +++ sample/triangle/test/10_lesses/cmd | 3 +++ sample/triangle/test/11_zeroall/0.txt | 2 +- sample/triangle/test/12_notint/0.txt | 2 +- sample/triangle/triangle.c | 16 +++++++++++----- sample/triangle/triangle_bug.c | 17 +++++++++++------ 10 files changed, 38 insertions(+), 15 deletions(-) diff --git a/sample/triangle/test/05_zero/0.txt b/sample/triangle/test/05_zero/0.txt index e371bce..0456fff 100644 --- a/sample/triangle/test/05_zero/0.txt +++ b/sample/triangle/test/05_zero/0.txt @@ -1 +1 @@ -0: invalid argument. +not triangle diff --git a/sample/triangle/test/06_minus/0.txt b/sample/triangle/test/06_minus/0.txt index a275577..2c240ac 100644 --- a/sample/triangle/test/06_minus/0.txt +++ b/sample/triangle/test/06_minus/0.txt @@ -1 +1 @@ --1: invalid argument. +-1: less than zero diff --git a/sample/triangle/test/08_lines/0.txt b/sample/triangle/test/08_lines/0.txt index a562ff8..e3af1aa 100644 --- a/sample/triangle/test/08_lines/0.txt +++ b/sample/triangle/test/08_lines/0.txt @@ -1,3 +1,6 @@ not triangle not triangle not triangle +not triangle +not triangle +not triangle diff --git a/sample/triangle/test/08_lines/cmd b/sample/triangle/test/08_lines/cmd index aea1c3d..c7dfd96 100755 --- a/sample/triangle/test/08_lines/cmd +++ b/sample/triangle/test/08_lines/cmd @@ -1,3 +1,6 @@ ../../triangle 1 2 3 ../../triangle 1 3 2 +../../triangle 2 1 3 +../../triangle 2 3 1 ../../triangle 3 1 2 +../../triangle 3 2 1 diff --git a/sample/triangle/test/10_lesses/0.txt b/sample/triangle/test/10_lesses/0.txt index a562ff8..e3af1aa 100644 --- a/sample/triangle/test/10_lesses/0.txt +++ b/sample/triangle/test/10_lesses/0.txt @@ -1,3 +1,6 @@ not triangle not triangle not triangle +not triangle +not triangle +not triangle diff --git a/sample/triangle/test/10_lesses/cmd b/sample/triangle/test/10_lesses/cmd index 8056645..9f3ced4 100755 --- a/sample/triangle/test/10_lesses/cmd +++ b/sample/triangle/test/10_lesses/cmd @@ -1,3 +1,6 @@ ../../triangle 1 2 4 ../../triangle 1 4 2 +../../triangle 2 4 1 +../../triangle 2 1 4 +../../triangle 4 1 2 ../../triangle 4 2 1 diff --git a/sample/triangle/test/11_zeroall/0.txt b/sample/triangle/test/11_zeroall/0.txt index e371bce..0456fff 100644 --- a/sample/triangle/test/11_zeroall/0.txt +++ b/sample/triangle/test/11_zeroall/0.txt @@ -1 +1 @@ -0: invalid argument. +not triangle diff --git a/sample/triangle/test/12_notint/0.txt b/sample/triangle/test/12_notint/0.txt index a891c01..259bbde 100644 --- a/sample/triangle/test/12_notint/0.txt +++ b/sample/triangle/test/12_notint/0.txt @@ -1 +1 @@ -3.1: invalid argument. +3.1: not integer diff --git a/sample/triangle/triangle.c b/sample/triangle/triangle.c index 9065bfa..3899e27 100644 --- a/sample/triangle/triangle.c +++ b/sample/triangle/triangle.c @@ -19,7 +19,7 @@ int main(int argc, char *argv[]) int l[3], i; char *check; - /* 引数が3以外の場合は、エラー終了 */ + /* 引数が3つ以外の場合は、エラー終了 */ if(argc != 4) { if(argc < 4) { fprintf(stderr, "too few argument.\n"); @@ -29,11 +29,17 @@ int main(int argc, char *argv[]) } return -1; } - /* 引数が自然数かチェック。自然数以外の場合はエラー終了 */ for(i = 0; i < 3; i++) { + /* 引数を整数に変換 */ l[i] = strtol(argv[i+1], &check, 10); - if(*check != '\0' || l[i] <= 0) { - fprintf(stderr, "%s: invalid argument.\n", argv[i+1]); + /* 整数以外の場合はエラー終了 */ + if(*check != '\0') { + fprintf(stderr, "%s: not integer\n", argv[i+1]); + return -1; + } + /* 0未満の場合はエラー終了 */ + if(l[i] < 0) { + fprintf(stderr, "%s: less than zero\n", argv[i+1]); return -1; } } @@ -47,7 +53,7 @@ int main(int argc, char *argv[]) puts("equilateral triangle"); } /* 2辺の長さが等しい場合は、二等辺三角形 */ - else if (l[0] == l[1] || l[1] == l[2] || l[2] == l[0]) { + else if(l[0] == l[1] || l[1] == l[2] || l[2] == l[0]) { puts("isosceles triangle"); } /* それ以外の場合は、不等辺三角形 */ diff --git a/sample/triangle/triangle_bug.c b/sample/triangle/triangle_bug.c index f4afd0d..91403fd 100644 --- a/sample/triangle/triangle_bug.c +++ b/sample/triangle/triangle_bug.c @@ -1,11 +1,10 @@ /********************************************************************** -triangle_bug.c +triangle.c 引数として指定された3つの整数が三角形の3辺を表すものとし、 次のうちどれであるかをきめるメッセージを印字する。 不等辺三角形(scalene triangle) 二等辺三角形(isosceles triangle) 正三角形(equilateral triangle) -ただし、バグを含む。 Glenford J Myers『ソフトウェア・テストの技法』(近代科学社、1980) http://www.amazon.co.jp/dp/4764900599 @@ -20,7 +19,7 @@ int main(int argc, char *argv[]) int l[3], i; char *check; - /* 引数が3以外の場合は、エラー終了 */ + /* 引数が3つ以外の場合は、エラー終了 */ if(argc != 4) { if(argc < 4) { fprintf(stderr, "too few argument.\n"); @@ -30,11 +29,17 @@ int main(int argc, char *argv[]) } return -1; } - /* 引数が0以上の整数かチェック。0以上の整数以外の場合はエラー終了 */ for(i = 0; i < 3; i++) { + /* 引数を整数に変換 */ l[i] = strtol(argv[i+1], &check, 10); - if(*check != '\0' || l[i] < 0) { - fprintf(stderr, "%s: invalid argument.\n", argv[i+1]); + /* 整数以外の場合はエラー終了 */ + if(*check != '\0') { + fprintf(stderr, "%s: not integer\n", argv[i+1]); + return -1; + } + /* 0未満の場合はエラー終了 */ + if(l[i] < 0) { + fprintf(stderr, "%s: less than zero\n", argv[i+1]); return -1; } } -- 2.18.0