/
Ch01Code.txt
119 lines (110 loc) · 3.95 KB
/
Ch01Code.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Chapter 1 - API Testing
// Example Program: ApiTest
using System;
using System.IO;
using MathLib; // houses methods under test
namespace TestAutomation
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
FileStream ifs = new FileStream("..\\..\\TestCases.txt",
FileMode.Open);
StreamReader sr = new StreamReader(ifs);
string stamp = DateTime.Now.ToString("s");
stamp = stamp.Replace(":", "-");
FileStream ofs = new FileStream("..\\..\\TestResults" +
stamp + ".txt", FileMode.CreateNew);
StreamWriter sw = new StreamWriter(ofs);
string line, caseID, method;
string[] tokens, tempInput;
string expected;
double actual = 0.0;
int numPass = 0, numFail = 0;
Console.WriteLine("\nCaseID Result Method Details");
Console.WriteLine("================================\n");
while ((line = sr.ReadLine()) != null)
{
tokens = line.Split(':');
caseID = tokens[0];
method = tokens[1];
tempInput = tokens[2].Split(' ');
expected = tokens[3];
int[] input = new int[tempInput.Length];
for (int i = 0; i < input.Length; ++i)
input[i] = int.Parse(tempInput[i]);
if (method == "ArithmeticMean")
{
actual = MathLib.Methods.ArithmeticMean(input);
if (actual.ToString("F4") == expected)
{
Console.WriteLine(caseID + " Pass " + method +
" actual = " + actual.ToString("F4"));
sw.WriteLine(caseID + " Pass " + method +
" actual = " + actual.ToString("F4"));
++numPass;
}
else
{
Console.WriteLine(caseID + " *FAIL* " + method +
" actual = " + actual.ToString("F4") +
" expected = " + expected);
sw.WriteLine(caseID + " *FAIL* " + method +
" actual = " + actual.ToString("F4") +
" expected = " + expected);
++numFail;
}
}
else if (method == "GeometricMean ")
{
MathLib.Methods m = new MathLib.Methods();
actual = m.GeometricMean(input);
if (actual.ToString("F4") == expected)
{
Console.WriteLine(caseID + " Pass " + method +
" actual = " + actual.ToString("F4"));
sw.WriteLine(caseID + " Pass " + method +
" actual = " + actual.ToString("F4"));
++numPass;
}
else
{
Console.WriteLine(caseID + " *FAIL* " + method +
" actual = " + actual.ToString("F4") +
" expected = " + expected);
sw.WriteLine(caseID + " *FAIL* " + method +
" actual = " + actual.ToString("F4") +
" expected = " + expected);
++numFail;
}
}
else
{
Console.WriteLine(caseID + " " + method +
" Not yet implemented");
sw.WriteLine(caseID + " " + method +
" Not yet implemented");
}
} // test case loop
Console.WriteLine("\n========= end test run ==========");
Console.WriteLine("\nPass = " + numPass +
" Fail = " + numFail);
sw.WriteLine(Environment.NewLine + "Pass = " + numPass +
" Fail = " + numFail);
sr.Close();
ifs.Close();
sw.Close();
ofs.Close();
}
catch(Exception ex)
{
Console.WriteLine("Fatal error: " + ex.Message);
}
Console.ReadLine();
} // Main()
} // class Class1
} // ns TestAutomation