astyle --style=ansi

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@373 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2005-11-29 13:20:54 +00:00
parent 3b596259ec
commit fa05da94c0

View file

@ -4,8 +4,8 @@
// purpose is hereby granted without fee, provided that the above copyright // purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this // notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation. // permission notice appear in supporting documentation.
// The author makes no representations about the suitability of this software // The author makes no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied // for any purpose. It is provided "as is" without express or implied
// warranty. // warranty.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -22,20 +22,21 @@ Integral2 RandomInt(Integral1 low, Integral2 up)
// From ``Accelerated C++'', page 135: // From ``Accelerated C++'', page 135:
// random integer in the range [0, n) // random integer in the range [0, n)
// We adjust to generate in the range [0, n] // We adjust to generate in the range [0, n]
const Integral2 const Integral2
low2 = low, low2 = low,
n = up - low; n = up - low;
assert(n > 0); assert(n > 0);
const unsigned int bucket_size = RAND_MAX / n; const unsigned int bucket_size = RAND_MAX / n;
assert(bucket_size > 0); assert(bucket_size > 0);
Integral2 r; Integral2 r;
do r = Integral2(rand() / bucket_size); do
r = Integral2(rand() / bucket_size);
while (r > n); while (r > n);
r += low2; r += low2;
assert(r >= low2 && r <= up); assert(r >= low2 && r <= up);
return r; return r;
} }
@ -44,77 +45,88 @@ string RandomString(unsigned int maxSize)
{ {
string result(RandomInt(0, maxSize), '\0'); string result(RandomInt(0, maxSize), '\0');
unsigned int i = 0; unsigned int i = 0;
for (; i != result.size(); ++i) { for (; i != result.size(); ++i)
{
result[i] = RandomInt('a', 'z'); result[i] = RandomInt('a', 'z');
} }
return result; return result;
} }
template <class T> template <class T>
void TestCase(const string& fmt, T value) { void TestCase(const string& fmt, T value)
{
char buf[4096]; char buf[4096];
std::string s; std::string s;
const int i1 = SPrintf(s, fmt.c_str())(value); const int i1 = SPrintf(s, fmt.c_str())(value);
#ifdef _MSC_VER
const int i2 = _snprintf(buf, sizeof(buf), fmt.c_str(), value);
#else
const int i2 = snprintf(buf, sizeof(buf), fmt.c_str(), value);
#endif
if (i1 != i2 || s != buf) #ifdef _MSC_VER
const int i2 = _snprintf(buf, sizeof(buf), fmt.c_str(), value);
#else
const int i2 = snprintf(buf, sizeof(buf), fmt.c_str(), value);
#endif
if (i1 != i2 || s != buf)
{ {
cout << endl cout << endl
<< "Reference: " << i2 << "; Actual: " << i1 << ", Difference = " << i2-i1 << endl << "Reference: " << i2 << "; Actual: " << i1 << ", Difference = " << i2-i1 << endl
<< "V: [" << value << "]" << endl << "V: [" << value << "]" << endl
<< "F: [" << fmt << "]" << endl << "F: [" << fmt << "]" << endl
<< "R: [" << buf << "]" << endl << "R: [" << buf << "]" << endl
<< "A: [" << s.c_str() << "]" << endl; << "A: [" << s.c_str() << "]" << endl;
assert(false); assert(false);
} }
} }
template <class T, class U> template <class T, class U>
void TestCase2(const string& fmt, T value, U value2) { void TestCase2(const string& fmt, T value, U value2)
{
char buf[4096]; char buf[4096];
std::string s; std::string s;
const int i1 = SPrintf(s, fmt.c_str())(value)(value2); const int i1 = SPrintf(s, fmt.c_str())(value)(value2);
const int i2 = snprintf(buf, sizeof(buf), fmt.c_str(), value, value2); const int i2 = snprintf(buf, sizeof(buf), fmt.c_str(), value, value2);
assert(i1 == i2); assert(i1 == i2);
assert(s == buf); assert(s == buf);
} }
int main(int argc, char** argv) { int main(int argc, char** argv)
if (argc == 3) { {
if (argc == 3)
{
// test speed // test speed
int i = atoi(argv[2]); int i = atoi(argv[2]);
switch (argv[1][0]) { switch (argv[1][0])
case 'p': {
for (; i > 0; --i) { case 'p':
printf("Hey, %u frobnicators and %u twiddlicators\n", for (; i > 0; --i)
i, i); {
} printf("Hey, %u frobnicators and %u twiddlicators\n",
break; i, i);
case 's': }
for (; i > 0; --i) { break;
cout << "Hey, " << i << " frobnicators and " << i << case 's':
" twiddlicators\n"; for (; i > 0; --i)
} {
break; cout << "Hey, " << i << " frobnicators and " << i <<
case 'n': " twiddlicators\n";
for (; i > 0; --i) { }
Printf("Hey, %u frobnicators and %u twiddlicators\n") break;
(i)(i); case 'n':
} for (; i > 0; --i)
break; {
Printf("Hey, %u frobnicators and %u twiddlicators\n")
(i)(i);
}
break;
} }
return 0; return 0;
} }
//srand(time(0)); //srand(time(0));
srand(0); srand(0);
for (unsigned i = 0; ; ++i) { for (unsigned i = 0; ; ++i)
{
printf("%u\r", i); printf("%u\r", i);
// Generate a random string for the head // Generate a random string for the head
string lead = RandomString(100); string lead = RandomString(100);
// This string will hold a random format specification // This string will hold a random format specification
@ -122,18 +134,21 @@ int main(int argc, char** argv) {
// Generate a random set of flags // Generate a random set of flags
static const string flags("-+0 #"); static const string flags("-+0 #");
unsigned int maxFlags = RandomInt(0u, flags.length() - 1); unsigned int maxFlags = RandomInt(0u, flags.length() - 1);
for (unsigned int i = 0; i != maxFlags; ++i) { for (unsigned int i = 0; i != maxFlags; ++i)
{
formatSpec += flags[RandomInt(0u, flags.length() - 1)]; formatSpec += flags[RandomInt(0u, flags.length() - 1)];
} }
// Generate an optional random width // Generate an optional random width
if (RandomInt(0, 1)) { if (RandomInt(0, 1))
{
const unsigned int width = RandomInt(0, 100); const unsigned int width = RandomInt(0, 100);
char buf[4]; char buf[4];
sprintf(buf, "%u", width); sprintf(buf, "%u", width);
formatSpec += buf; formatSpec += buf;
} }
// Generate an optional random precision // Generate an optional random precision
if (RandomInt(0, 1)) { if (RandomInt(0, 1))
{
const unsigned int prec = RandomInt(0, 100); const unsigned int prec = RandomInt(0, 100);
char buf[4]; char buf[4];
sprintf(buf, "%u", prec); sprintf(buf, "%u", prec);
@ -145,39 +160,50 @@ int main(int argc, char** argv) {
const char typeSpec = type[RandomInt(0, type.size() - 1)]; const char typeSpec = type[RandomInt(0, type.size() - 1)];
// Generate an optional type prefix // Generate an optional type prefix
static const string prefix("hl"); static const string prefix("hl");
if (typeSpec != 's' && RandomInt(0, 1)) { if (typeSpec != 's' && RandomInt(0, 1))
{
formatSpec += prefix[RandomInt(0, prefix.size() - 1)]; formatSpec += prefix[RandomInt(0, prefix.size() - 1)];
} }
formatSpec += typeSpec; formatSpec += typeSpec;
formatSpec += '|'; formatSpec += '|';
formatSpec += RandomString(100); formatSpec += RandomString(100);
switch (typeSpec) { switch (typeSpec)
case 'c': {
case 'c':
TestCase(formatSpec, RandomInt(1, 127)); TestCase(formatSpec, RandomInt(1, 127));
break; break;
case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
TestCase(formatSpec, RandomInt(-10000, 10000)); TestCase(formatSpec, RandomInt(-10000, 10000));
break; break;
case 'e': case 'E': case 'f': case 'g': case 'G': case 'e':
TestCase(formatSpec, case 'E':
RandomInt(-10000, 10000) / double(RandomInt(1, 100))); case 'f':
case 'g':
case 'G':
TestCase(formatSpec,
RandomInt(-10000, 10000) / double(RandomInt(1, 100)));
break; break;
case 'n': case 'n':
break; break;
case 'p': case 'p':
{ {
void * p = malloc(RandomInt(1, 1000)); void * p = malloc(RandomInt(1, 1000));
TestCase(formatSpec, p); TestCase(formatSpec, p);
free(p); free(p);
} }
break; break;
case 's': case 's':
TestCase(formatSpec, RandomString(100).c_str()); TestCase(formatSpec, RandomString(100).c_str());
break; break;
default: default:
assert(false); assert(false);
break; break;
} }
} }
} }