main.cpp
6.79 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* This software was written by people from OnShore Consulting services LLC
* <info@sabgroup.com> and placed in the public domain.
*
* We reserve no legal rights to any of this. You are free to do
* whatever you want with it. And we make no guarantee or accept
* any claims on damages as a result of this.
*
* If you change the software, please help us and others improve the
* code by sending your modifications to us. If you choose to do so,
* your changes will be included under this license, and we will add
* your name to the list of contributors.
*/
#include <QCoreApplication>
#include <QtTest>
#include <QDebug>
#include "NaturalStringCompare.h"
class CTestNaturalStringCompare : public QObject
{
Q_OBJECT
private slots:
void compareString()
{
QString str1 = "abcdef";
QString str2 = "aabc";
QVERIFY( NaturalStringCompare(str1,str2) > 0 );
QVERIFY( NaturalStringCompare(str2,str1) < 0 );
QVERIFY( NaturalStringCompare(str1,str1) == 0 );
QVERIFY( NaturalStringCompare(str2,str2) == 0 );
QVERIFY( NaturalStringCompare(str1,str1.left(2)) > 0 );
QVERIFY( NaturalStringCompare(str1.left(2),str1) < 0 );
}
void compareInteger()
{
for( int ii = -15; ii <= 15; ii++ )
{
QString currVal = QString( "%1" ).arg( ii );
QString nextVal = QString( "%1" ).arg( ii+1 );
QVERIFY( NaturalStringCompare(currVal,nextVal) < 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal, "prefix" + nextVal) < 0 );
QVERIFY( NaturalStringCompare( currVal + "postfix", nextVal + "postfix") < 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal + "postfix", "prefix" + nextVal + "postfix") < 0 );
QVERIFY( NaturalStringCompare(nextVal,currVal) > 0 );
QVERIFY( NaturalStringCompare( "prefix" + nextVal, "prefix" + currVal) > 0 );
QVERIFY( NaturalStringCompare( nextVal + "postfix", currVal + "postfix") > 0 );
QVERIFY( NaturalStringCompare( "prefix" + nextVal + "postfix", "prefix" + currVal + "postfix") > 0 );
QVERIFY( NaturalStringCompare(currVal,currVal) == 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal, "prefix" + currVal) == 0 );
QVERIFY( NaturalStringCompare( currVal + "postfix", currVal + "postfix") == 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal + "postfix", "prefix" + currVal + "postfix") == 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal + "middle" + currVal, "prefix" + currVal + "middle" + nextVal ) < 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal + "middle" + nextVal, "prefix" + currVal + "middle" + currVal ) > 0 );
}
}
void compareDouble()
{
for( int ii = -15; ii <= 15; ii++ )
{
QString currVal = QString( "%1" ).arg( 1.0*ii - 0.05 );
QString nextVal = QString( "%1" ).arg( 1.0*ii + 0.05 );
QVERIFY( NaturalStringCompare(currVal,nextVal) < 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal, "prefix" + nextVal) < 0 );
QVERIFY( NaturalStringCompare( currVal + "postfix", nextVal + "postfix") < 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal + "postfix", "prefix" + nextVal + "postfix") < 0 );
QVERIFY( NaturalStringCompare(nextVal,currVal) > 0 );
QVERIFY( NaturalStringCompare( "prefix" + nextVal, "prefix" + currVal) > 0 );
QVERIFY( NaturalStringCompare( nextVal + "postfix", currVal + "postfix") > 0 );
QVERIFY( NaturalStringCompare( "prefix" + nextVal + "postfix", "prefix" + currVal + "postfix") > 0 );
QVERIFY( NaturalStringCompare(currVal,currVal) == 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal, "prefix" + currVal) == 0 );
QVERIFY( NaturalStringCompare( currVal + "postfix", currVal + "postfix") == 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal + "postfix", "prefix" + currVal + "postfix") == 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal + "middle" + currVal, "prefix" + currVal + "middle" + nextVal ) < 0 );
QVERIFY( NaturalStringCompare( "prefix" + currVal + "middle" + nextVal, "prefix" + currVal + "middle" + currVal ) > 0 );
}
}
void sortStringList()
{
QStringList orig;
for( int ii = 15; ii >= -15; ii-- )
{
QString currVal = QString( "%1" ).arg( 1.0*ii - 0.05 );
orig << "prefix" + currVal + "postfix";
orig << "Prefix" + currVal + "posTfIx";
currVal = QString( "%1" ).arg( ii );
orig << "Prefix" + currVal;
orig << currVal + "PostFIX";
orig << currVal + "PostFix";
}
QStringList toBeSorted = orig;
qSort( toBeSorted.begin(), toBeSorted.end(), NaturalStringCompareLessThan );
int cnt = toBeSorted.count();
QVERIFY( toBeSorted.count() == 31 * 5 );
QVERIFY( toBeSorted.front() == "-15PostFIX" );
QVERIFY( toBeSorted.back() == "prefix14.95postfix" );
QStringList tmp = NaturalStringSort( orig );
QVERIFY( tmp.count() == toBeSorted.count() );
for( int ii = 0; ii < tmp.count(); ++ii )
{
QVERIFY( tmp[ ii ] == toBeSorted[ ii ] );
}
toBeSorted = orig;
qSort( toBeSorted.begin(), toBeSorted.end(), NaturalStringCaseInsensitiveCompareLessThan );
cnt = toBeSorted.count();
QVERIFY( toBeSorted.count() == 31 * 5 );
// QVERIFY( toBeSorted.front() == "-15PostFix" ); for case insensitive, PostFix vs PostFIX is non-deterministic which comes firs
// QVERIFY( toBeSorted.back() == "Prefix15" );
tmp = NaturalStringSort( orig, Qt::CaseInsensitive );
QVERIFY( tmp.count() == toBeSorted.count() );
for( int ii = 0; ii < tmp.count(); ++ii )
{
QVERIFY( tmp[ ii ] == toBeSorted[ ii ] );
}
}
void lingfaYangTest()
{
// this is actually not what the spec wants, however, the 8 causes the first compare to be
// ppt/slides/slide vs ppt/slides/slide.xml which means embedded numbers will come first.
QVERIFY( NaturalStringCompare( "ppt/slides/slide8.xml", "ppt/slides/slide.xml2", Qt::CaseInsensitive ) > 0 );
QStringList orderedList = QStringList() // from Ligfa Ya email
<< "[Content_Types].xml"
<< "_rels/.rels"
<< "ppt/media/image8.wmf"
<< "ppt/media/image9.jpeg"
<< "ppt/media/image10.png"
<< "ppt/media/image11.gif"
<< "ppt/slides/_rels/slide9.xml.rels"
<< "ppt/slides/_rels/slide10.xml.rels"
<< "ppt/slides/slide.xml"
<< "ppt/slides/slide8.xml"
<< "PPT/SLIDES/SLIDE9.XML"
<< "ppt/slides/slide10.xml"
<< "ppt/slides/slide11.xml"
<< "slide.xml"
;
QStringList toBeSorted = QStringList();
QStringList tmp = orderedList;
qsrand( QDateTime::currentDateTime().toTime_t() );
while( !tmp.isEmpty() )
{
double randVal = qrand();
randVal /= RAND_MAX;
int val = (int)( tmp.count() - 1 ) * randVal;
toBeSorted << tmp[ val ];
tmp.removeAt( val );
}
tmp = NaturalStringSort( toBeSorted, Qt::CaseInsensitive );
for( int ii = 0; ii < tmp.count(); ++ii )
{
QVERIFY( tmp[ ii ] == orderedList[ ii ] );
}
}
};
QTEST_MAIN(CTestNaturalStringCompare)
#include "main.moc"