1 // This is core/vul/tests/test_awk.cxx
2 #include <sstream>
3 #include <iostream>
4 #include "vul/vul_awk.h"
5 #include "testlib/testlib_test.h"
6 //:
7 // \file
8 // \brief  Rudimentary testing of the vul_awk class
9 // \author Eric Moyer (Wright State University)
10 // \date   15 July 2009
11 // History: built upon the shell of test_regexp, but has no tests in common
12 
13 #ifdef _MSC_VER
14 #  include "vcl_msvc_warnings.h"
15 #endif
16 
17 void
test_awk()18 test_awk()
19 {
20   // Test empty file
21   {
22     std::stringstream tmp;
23     tmp << "";
24     std::stringstream in(tmp.str().c_str());
25     vul_awk awk(in);
26     TEST("empty:awk on empty file is invalid", !awk, true);
27     TEST("empty:awk on creation has line 1", awk.NR(), 1);
28   }
29 
30   // Test easy file
31   {
32     std::stringstream tmp;
33     tmp << "1a 1b 1c\n"
34         << "2a 2b 2c\n"
35         << "  3a  3b 3c" << std::endl;
36     std::stringstream in(tmp.str().c_str());
37     vul_awk awk(in);
38     TEST("easy:awk on non-empty file is valid", (bool)awk, true);
39     TEST("easy:awk on creation has line 1", awk.NR(), 1);
40     char letter[] = "abc";
41     for (int line = 0; line < 3; ++line)
42     {
43       {
44         std::stringstream msg;
45         msg << "easy:awk has correct # of fields for line:" << (line + 1);
46         TEST(msg.str().c_str(), awk.NF(), 3);
47       }
48       for (int i = 0; i < 3; ++i)
49       {
50         std::stringstream msg;
51         msg << "easy:correct field content line:" << (line + 1) << " field: " << i;
52         std::stringstream expected;
53         expected << (line + 1) << letter[i];
54         TEST(msg.str().c_str(), expected.str(), awk[i]);
55       }
56       ++awk;
57       {
58         std::stringstream msg;
59         msg << "easy:awk has correct line number for line:" << (line + 2);
60         TEST(msg.str().c_str(), awk.NR(), line + 2);
61       }
62     }
63     TEST("easy:awk at end of file is invalid", !awk, true);
64   }
65 
66   // Test commented file with different number of fields per line
67   {
68     std::stringstream tmp;
69     tmp << "#Commented test file\n"
70         << "2a 2b 2c #\n"
71         << "3a 3b 3c 3d 3e #3f\n"
72         << "#Another comment in the middle\n"
73         << "  5a # Just one field and comment\n"
74         << "  6a\n"
75         << "#Last line is a comment" << std::endl;
76     std::stringstream in(tmp.str().c_str());
77     vul_awk awk(in, vul_awk::strip_comments);
78     TEST("comment:awk on non-empty file is valid", (bool)awk, true);
79     TEST("comment:awk on creation has line 2", awk.NR(), 2);
80     char letter[] = "abcdef";
81     // lineNum[numIncrements] has the expected line in the file number
82     // after that number of increments have occurred
83     int lineNum[] = { 2, 3, 5, 6, 8 };
84     // numFields[line] as the number of fields in that line
85     int numFields[] = { -1, -1, 3, 5, -1, 1, 1, -1 };
86     for (int numIncrements = 0; numIncrements < 4; ++numIncrements)
87     {
88       int line = lineNum[numIncrements];
89       {
90         std::stringstream msg;
91         msg << "comment:awk has correct # of fields for line:" << line;
92         TEST(msg.str().c_str(), awk.NF(), numFields[line]);
93       }
94       for (int i = 0; i < numFields[line]; ++i)
95       {
96         std::stringstream msg;
97         msg << "comment:correct field content line:" << line << " field: " << i;
98         std::stringstream expected;
99         expected << line << letter[i];
100         TEST(msg.str().c_str(), expected.str(), awk[i]);
101       }
102       ++awk;
103       {
104         std::stringstream msg;
105         msg << "comment:awk has correct line number after " << (numIncrements + 1) << " increments.";
106         TEST(msg.str().c_str(), awk.NR(), lineNum[numIncrements + 1]);
107       }
108     }
109     TEST("comment:awk at end of file is invalid", !awk, true);
110   }
111 }
112 
113 TEST_MAIN(test_awk)
114