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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// Unit test for NameType class
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include "NameType.h"
static const int Err(const char* msg) {
fprintf(stderr, "Error: %s\n", msg);
return 1;
}
struct reverseSort {
bool operator() (const NameType& lhs, const NameType& rhs) const {
return ( lhs > rhs );
}
} reverseSortObj;
int main() {
// This unit test is primarily for the matching.
NameType name1("CT11");
// Copy construction
NameType tmp("HA");
NameType name2(tmp);
// Test assignment
NameType name3;
name3 = name2;
// Test conversion from strings
std::string str4("HA2");
NameType name4( str4 );
std::string str5("ZN");
NameType name5( str5 );
// Copy construction
std::vector<NameType> Nvector;
Nvector.push_back( name3 );
Nvector.push_back( name5 );
Nvector.push_back( name4 );
Nvector.push_back( name1 );
Nvector.push_back( name2 );
// Test that assignment worked.
bool n1n2_eq = (name2 == name3);
if (!n1n2_eq) return Err("'==' operator failed after assignment.\n");
bool n1n2_ne = (name2 != name3);
if (n1n2_ne) return Err("'!=' operator failed after assignment.\n");
n1n2_eq = (name2 == "HA");
if (!n1n2_eq) return Err("'==' const char* operator failed after assignment.\n");
n1n2_ne = (name2 != "HA");
if (n1n2_ne) return Err("'!=' const char* operator failed after assignment.\n");
// Sort. Tests the < operator.
std::sort( Nvector.begin(), Nvector.end() );
// Test that copy construction/sort worked.
if ( (name1 != Nvector[0]) ||
(name2 != Nvector[1]) ||
(name3 != Nvector[2]) ||
(name4 != Nvector[3]) ||
(name5 != Nvector[4]) )
return Err("Sort/copy construction failed (order is wrong).\n");
// Reverse sort. Tests the > operator.
std::sort( Nvector.begin(), Nvector.end(), reverseSortObj );
// Test that reverse sort worked.
if ( (name5 != Nvector[0]) ||
(name4 != Nvector[1]) ||
(name3 != Nvector[2]) ||
(name2 != Nvector[3]) ||
(name1 != Nvector[4]) )
return Err("Reverse sort failed (order is wrong).\n");
// Write to stdout
for (std::vector<NameType>::const_iterator it = Nvector.begin(); it != Nvector.end(); ++it)
printf(" '%s'", *(*it));
printf("\n");
// Test that an out of range index is properly caught
char testchar1 = name1[-1];
char testchar2 = name1[999999];
if (testchar1 != '\0' || testchar2 != '\0')
return Err("[] operator did not return null char for out of range index.\n");
// Test length function
if (name1.len() != 4 ||
name2.len() != 2 ||
name3.len() != 2 ||
name4.len() != 3 ||
name5.len() != 2)
return Err("len() function failed.\n");
// Test single wildcard matching for name1 'CT11'
bool swc1 = name1.Match("?T11");
bool swc2 = name1.Match("C?11");
bool swc3 = name1.Match("CT?1");
bool swc4 = name1.Match("CT1?");
bool swc5 = name1.Match("?T1?");
if (!swc1 ||
!swc2 ||
!swc3 ||
!swc4 ||
!swc5)
return Err("Single wildcard matching failed.\n");
// Test wildcard matching for name1 'CT11'
bool wc1 = name1.Match("C*");
bool wc2 = name1.Match("C*1");
bool wc3 = name1.Match("CT*");
if (!wc1 ||
!wc2 ||
!wc3)
return Err("Wildcard matching failed.\n");
// Test mixed wildcard matching
bool mwc1 = name1.Match("*1?");
if (!mwc1)
return Err("Mixed wildcard matching failed.\n");
// Check that large name produces a warning
NameType large1("ThisIsALargeName");
NameType large2( std::string("ThisIsALargeString") );
// This should not produce a warning
NameType char5("12345");
// Test matching with asterisk in name.
NameType name6("O5*1");
bool a1wc = name6.Match("O5*"); // Match
bool a2wc = name6.Match("O5\\*1"); // Match
bool a3wc = name6.Match("O5\\*?"); // Match
if (!a1wc ||
!a2wc ||
!a3wc)
return Err("Matching name with asterisk.\n");
NameType name7("O541");
bool a4wc = name7.Match("O5*"); // Match
bool a5wc = name7.Match("O5\\*1"); // No match
bool a6wc = name7.Match("O5\\*?"); // No Match
if (!a4wc ||
a5wc ||
a6wc)
return Err("Matching name without asterisk.\n");
return 0;
}
|