.autotest

git-svn-id: svn://ultimatepp.org/upp/trunk@9765 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2016-05-02 21:13:08 +00:00
parent d657e153f2
commit dfc5163d5f
10 changed files with 24 additions and 234 deletions

View file

@ -6,20 +6,24 @@ using namespace Upp;
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_COUT|LOG_FILE);
Vector<int> x;
x << 1 << 5 << 4 << 3 << 2 << 10;
// 0 1 2 3 4 5
Check(FindMin(x, 0, 2), 0);
Check(FindMax(x, 0, 3), 1);
Check(FindMax(x, 2, 3), 2);
Check(FindMax(x, 2, 4), 5);
Check(FindMin(x, 2, 4), 4);
Check(FindMin(SubRange(x, 0, 2)), 0);
Check(FindMax(SubRange(x, 0, 3)), 1);
Check(FindMax(SubRange(x, 2, 3)) + 2, 2);
Check(FindMax(SubRange(x, 2, 4)) + 2, 5);
Check(FindMin(SubRange(x, 2, 4)) + 2, 4);
Sort(x);
Check(FindBinaryIter(x.Begin(), x.End(), 1) - x.Begin(), 0);
Check(FindBinaryIter(x.Begin(), x.End(), 2) - x.Begin(), 1);
Check(FindBinaryIter(x.Begin(), x.End(), 7), NULL);
Check(FindBinaryIter(x.Begin(), x.End(), 0), NULL);
Check(FindBinaryIter(x.Begin(), x.End(), 11), NULL);
Check(FindBinaryIter(x.Begin(), x.End(), 10) - x.Begin(), 5);
Check(FindBinary(x, 1), 0);
Check(FindBinary(x, 2), 1);
Check(FindBinary(x, 7), -1);
Check(FindBinary(x, 0), -1);
Check(FindBinary(x, 11), -1);
Check(FindBinary(x, 10), 5);
LOG("================ OK");
}

View file

@ -3,139 +3,6 @@
using namespace Upp;
template <class I, class Less>
void Sort(CoWork& cw, I l, I h, const Less& less)
{
const int PARALLEL_THRESHOLD = 80;
for(;;) {
int count = int(h - l);
if(count < 2)
return;
if(count < 8) { // Final optimized SelectSort
ForwardSort(l, h, less);
return;
}
int pass = 4;
for(;;) {
I middle = l + (count >> 1); // get the middle element
OrderIter2__(l, middle, less); // sort l, middle, h-1 to find median of 3
OrderIter2__(middle, h - 1, less);
OrderIter2__(l, middle, less); // median is now in middle
IterSwap(l + 1, middle); // move median pivot to l + 1
I ii = l + 1;
for(I i = l + 2; i != h - 1; ++i) // do partitioning; already l <= pivot <= h - 1
if(less(*i, *(l + 1)))
IterSwap(++ii, i);
IterSwap(ii, l + 1); // put pivot back in between partitions
I iih = ii;
while(iih + 1 != h && !less(*ii, *(iih + 1))) // Find middle range of elements equal to pivot
++iih;
if(pass > 5 || min(ii - l, h - iih) > (max(ii - l, h - iih) >> pass)) { // partition sizes ok or we have done max attempts
if(ii - l < h - iih - 1) { // schedule or recurse on smaller partition, tail on larger
if(ii - l < PARALLEL_THRESHOLD) // too small to run in parallel?
Sort(l, ii, less); // resolve in this thread
else
cw & [=, &cw] { CoSort(cw, l, ii, less); }; // schedule for parallel execution
l = iih + 1;
}
else {
if(h - iih - 1 < PARALLEL_THRESHOLD) // too small to run in parallel?
Sort(iih + 1, h, less); // resolve in this thread
else
cw & [=, &cw] { CoSort(cw, iih + 1, h, less); }; // schedule for parallel execution
h = ii;
}
break;
}
IterSwap(l, l + (int)Random(count)); // try some other random elements for median pivot
IterSwap(middle, l + (int)Random(count));
IterSwap(h - 1, l + (int)Random(count));
pass++;
}
}
}
template <class I, class Less>
void CoSort(CoWork& cw, I l, I h, const Less& less)
{
const int PARALLEL_THRESHOLD = 80;
for(;;) {
int count = int(h - l);
if(count < 2)
return;
if(count < 8) { // Final optimized SelectSort
ForwardSort(l, h, less);
return;
}
int pass = 4;
for(;;) {
I middle = l + (count >> 1); // get the middle element
OrderIter2__(l, middle, less); // sort l, middle, h-1 to find median of 3
OrderIter2__(middle, h - 1, less);
OrderIter2__(l, middle, less); // median is now in middle
IterSwap(l + 1, middle); // move median pivot to l + 1
I ii = l + 1;
for(I i = l + 2; i != h - 1; ++i) // do partitioning; already l <= pivot <= h - 1
if(less(*i, *(l + 1)))
IterSwap(++ii, i);
IterSwap(ii, l + 1); // put pivot back in between partitions
I iih = ii;
while(iih + 1 != h && !less(*ii, *(iih + 1))) // Find middle range of elements equal to pivot
++iih;
// LOG("count: " << count << ", l: " << ii - l << ", h: " << h - iih);
if(pass > 5 || min(ii - l, h - iih) > (max(ii - l, h - iih) >> pass)) { // partition sizes ok or we have done max attempts
if(ii - l < h - iih - 1) { // schedule or recurse on smaller partition, tail on larger
if(ii - l < PARALLEL_THRESHOLD) // too small to run in parallel?
Sort(l, ii, less); // resolve in this thread
else {
LOG("Schedule " << count << ": " << ii - l);
cw & [=, &cw] { CoSort(cw, l, ii, less); }; // schedule for parallel execution
}
l = iih + 1;
}
else {
if(h - iih - 1 < PARALLEL_THRESHOLD) // too small to run in parallel?
Sort(iih + 1, h, less); // resolve in this thread
else {
LOG("Schedule " << count << ": " << h - iih - 1);
cw & [=, &cw] { CoSort(cw, iih + 1, h, less); }; // schedule for parallel execution
}
h = ii;
}
break;
}
// LOG("RETRY: " << count << ", l: " << ii - l << ", h: " << h - iih);
IterSwap(l, l + (int)Random(count)); // try some other random elements for median pivot
IterSwap(middle, l + (int)Random(count));
IterSwap(h - 1, l + (int)Random(count));
pass++;
}
}
}
template <class I, class Less>
void CoSort(I l, I h, const Less& less)
{
CoWork cw;
CoSort(cw, l, h, less);
}
template <class T, class Less>
void CoSort(T& c, const Less& less)
{
CoSort(c.Begin(), c.End(), less);
}
template <class T>
void CoSort(T& c)
{
typedef typename T::ValueType VT;
CoSort(c.Begin(), c.End(), StdLess<VT>());
}
#ifdef _DEBUG
#define N 100000
#else

View file

@ -64,7 +64,6 @@ CONSOLE_APP_MAIN
ArrayTest< InVector<int> > ();
ArrayTest< InArray<int> > ();
ArrayTest< Index<int> > ();
ArrayTest< ArrayIndex<int> > ();
ArrayTest< SortedIndex<int> > ();
BiArrayTest< BiVector<int> > ();

View file

@ -1,66 +0,0 @@
#include <Core/Core.h>
using namespace Upp;
// WARNING: 64-bit OS and 8GB RAM required to run this test
String RandomString(int maxlen = 70)
{
int len = Random(maxlen);
String h;
for(int i = 0; i < len; i++)
h.Cat(Random(26) + 'a');
return h;
}
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_COUT|LOG_FILE);
MemoryLimitKb(8000000);
for(int sz = 0; sz < 2; sz++) {
for(int pass = 0; pass < 2; pass++) {
LOG("--------------------");
DUMP(sz);
DUMP(pass);
{
NanoStrings ns;
Vector<dword> ws;
ns.ZeroTerminated(sz);
SeedRandom();
int i = 0;
int time0 = msecs();
while(msecs(time0) < 20000) {
if(++i % 10000000 == 0)
RLOG("Created " << i);
String s = pass ? "x" : RandomString(Random(4) ? 5 : 50);
ws.Add(ns.Add(s));
}
ns.DumpProfile();
LOG("---- Strings " << MemoryUsedKb() << " KB used -------");
LOG(MemoryProfile());
SeedRandom();
for(int i = 0; i < ws.GetCount(); i++) {
if(i % 10000000 == 0)
RLOG("Tested " << i);
String s = pass ? "x" : RandomString(Random(4) ? 5 : 50);
if((sz ? String(ns.GetPtr(ws[i])) : ns.Get(ws[i])) != s) {
DUMP(i);
DUMP(ns.Get(ws[i]));
DUMP(s);
NEVER();
}
}
LOG("Test OK");
}
LOG("===== EMPTY " << MemoryUsedKb() << " KB used -------");
LOG(MemoryProfile());
}
}
LOG("============ Everything OK ===================");
}

View file

@ -1,9 +0,0 @@
uses
Core;
file
NanoStrings.cpp;
mainconfig
"" = "";

View file

@ -1,4 +0,0 @@
#ifndef _NanoStrings_icpp_init_stub
#define _NanoStrings_icpp_init_stub
#include "Core/init"
#endif

View file

@ -48,11 +48,11 @@ CONSOLE_APP_MAIN
{
Vector<int> data = GetIntData();
LOG("int " << data.GetCount());
StableSortCmp(data);
StableSort(data);
CheckSorted(data);
Vector<String> sdata = GetStringData();
LOG("String " << sdata.GetCount());
StableSortCmp(sdata);
StableSort(sdata);
CheckSorted(sdata);
}
}

View file

@ -18,13 +18,13 @@ CONSOLE_APP_MAIN
Vector<Value> v;
v << 5 << 1 << 3;
ValueArray va = pick(v);
ASSERT(v.IsPicked());
ASSERT(v.GetCount() == 0);
TEST(va, "[5, 1, 3]");
Vector<Value> v2 = va.Pick();
TEST(v2, "[5, 1, 3]");
Sort(v2);
va = pick(v2);
ASSERT(v2.IsPicked());
ASSERT(v2.GetCount() == 0);
TEST(va, "[1, 3, 5]");
}
{

View file

@ -8,13 +8,13 @@ CONSOLE_APP_MAIN
XmlNode a = ParseXMLFile(GetDataFile("0.xml"));
XmlNode b = pick(a); // pick copy
ASSERT(a.IsPicked());
ASSERT(!b.IsPicked());
ASSERT(a.GetCount() == 0);
ASSERT(b.GetCount());
DDUMP(AsXML(b, XML_HEADER|XML_DOCTYPE));
a <<= b;
ASSERT(!a.IsPicked());
ASSERT(!b.IsPicked());
a = clone(b);
ASSERT(a.GetCount());
ASSERT(b.GetCount());
DDUMP(AsXML(a, XML_HEADER|XML_DOCTYPE));
DDUMP(AsXML(b, XML_HEADER|XML_DOCTYPE));
AsXML(b, XML_HEADER|XML_DOCTYPE);

View file

@ -47,7 +47,6 @@ CONSOLE_APP_MAIN
Test2<InVector<int>>();
Test2<Index<int>>();
Test2<ArrayIndex<int>>();
Test2<BiVector<int>>();
Test2<BiArray<int>>();