Commit ca9431f7 by Hans-Peter Nilsson Committed by Hans-Peter Nilsson

chi2_quality.h: Break out from chi2_quality.cc.

	* testsuite/20_util/hash/chi2_quality.h: Break out from
	chi2_quality.cc.
	* testsuite/20_util/hash/chi2_q_bit_flip_set.cc: Ditto.
	* testsuite/20_util/hash/chi2_q_document_words.cc: Ditto.
	* testsuite/20_util/hash/chi2_q_bit_string_set.cc: Ditto.  Increase
	SAMPLES to 35000 for simulator targets.
	* testsuite/20_util/hash/chi2_q_numeric_pattern_set.cc: Ditto.
	* testsuite/20_util/hash/chi2_q_uniform_random.cc: Ditto.
	* testsuite/20_util/hash/chi2_quality.cc: Remove.

From-SVN: r206167
parent 96758521
2013-12-22 Hans-Peter Nilsson <hp@axis.com>
* testsuite/20_util/hash/chi2_quality.h: Break out from
chi2_quality.cc.
* testsuite/20_util/hash/chi2_q_bit_flip_set.cc: Ditto.
* testsuite/20_util/hash/chi2_q_document_words.cc: Ditto.
* testsuite/20_util/hash/chi2_q_bit_string_set.cc: Ditto. Increase
SAMPLES to 35000 for simulator targets.
* testsuite/20_util/hash/chi2_q_numeric_pattern_set.cc: Ditto.
* testsuite/20_util/hash/chi2_q_uniform_random.cc: Ditto.
* testsuite/20_util/hash/chi2_quality.cc: Remove.
2013-12-10 Paolo Carlini <paolo.carlini@oracle.com>
* testsuite/20_util/is_base_of/value.cc: Add test.
......
// { dg-options "-std=gnu++0x" }
// Use smaller statistics when running on simulators, so it takes less time.
// { dg-options "-std=gnu++0x -DSAMPLES=30000" { target simulator } }
// Copyright (C) 2010-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "chi2_quality.h"
// Tests chi^2 for a distribution of strings that differ from each
// other by only a few bits. We start with an arbitrary base string, and
// flip three random bits for each member of the set.
void
test_bit_flip_set()
{
bool test __attribute__((unused)) = true;
const unsigned long N = SAMPLES;
const unsigned long k = N/100;
const unsigned int len = 67;
const unsigned int bitlen = len * 8;
const unsigned int bits_to_flip = 3;
const char base[len+1] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789!@#$%";
std::unordered_set<std::string> set;
while (set.size() < N)
{
std::string s(base, base+len);
for (unsigned int i = 0; i < bits_to_flip; ++i)
{
int bit = rand() % bitlen;
s[bit/8] ^= (1 << (bit%8));
}
set.insert(s);
}
double chi2 = chi2_hash(set, k);
VERIFY( chi2 < k*1.1 );
}
int
main()
{
test_bit_flip_set();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Use smaller statistics when running on simulators, so it takes less time.
// For e.g. cris-elf, mipsisa32r2el-elf, powerpc-eabi and i386-linux-gnu,
// this test fails for SAMPLES=30000.
// { dg-options "-std=gnu++0x -DSAMPLES=35000" { target simulator } }
// Copyright (C) 2010-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "chi2_quality.h"
// Tests chi^2 for a set of strings that all consist of '1' and '0'.
void
test_bit_string_set()
{
bool test __attribute__((unused)) = true;
const unsigned long N = SAMPLES;
const unsigned long k = N/100;
std::vector<std::string> set;
std::string s;
for (unsigned long i = 0; i < N; ++i)
{
s.clear();
for (unsigned int j = 0; j < sizeof(unsigned long) * 8; ++j)
{
const bool bit = (1UL << j) & i;
s.push_back(bit ? '1' : '0');
}
set.push_back(s);
}
double chi2 = chi2_hash(set, k);
VERIFY( chi2 < k*1.1 );
}
int
main()
{
test_bit_string_set();
return 0;
}
// On some simulators, the workload is simply too large with values big
// enough for the test to pass the quality test, so just skip it altogether.
// { dg-do run { target { ! simulator } } }
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2010-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "chi2_quality.h"
// Tests chi^2 for a set of words taken from a document written in English.
void
test_document_words()
{
bool test __attribute__((unused)) = true;
const std::string f_name = "thirty_years_among_the_dead_preproc.txt";
std::ifstream in(f_name);
VERIFY( in.is_open() );
std::vector<std::string> words;
words.assign(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>());
VERIFY( words.size() > 100000 );
std::sort(words.begin(), words.end());
auto it = std::unique(words.begin(), words.end());
words.erase(it, words.end());
VERIFY( words.size() > 5000 );
const unsigned long k = words.size() / 20;
double chi2 = chi2_hash(words, k);
VERIFY( chi2 < k*1.1 );
}
int
main()
{
test_document_words();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Use smaller statistics when running on simulators, so it takes less time.
// For x86_64-linux-gnu SAMPLES=30000 fails, so increase slightly.
// { dg-options "-std=gnu++0x -DSAMPLES=35000" { target simulator } }
// Copyright (C) 2010-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "chi2_quality.h"
// Tests chi^2 of a set of strings that all have a similar pattern,
// intended to mimic some sort of ID string.
void
test_numeric_pattern_set()
{
bool test __attribute__((unused)) = true;
const unsigned long N = SAMPLES;
const unsigned long k = N/100;
std::vector<std::string> set;
for (unsigned long i = 0; i < N; ++i)
{
long i1 = i % 100000;
long i2 = i / 100000;
char buf[16];
std::sprintf(buf, "XX-%05lu-%05lu", i1, i2);
set.push_back(buf);
}
double chi2 = chi2_hash(set, k);
VERIFY( chi2 < k*1.1 );
}
int
main()
{
test_numeric_pattern_set();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Use smaller statistics when running on simulators, so it takes less time.
// For powerpc-eabi, SAMPLES=30000 fails.
// { dg-options "-std=gnu++0x -DSAMPLES=35000" { target simulator } }
// Copyright (C) 2010-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "chi2_quality.h"
// Tests chi^2 for a distribution of uniformly generated random strings.
void
test_uniform_random()
{
bool test __attribute__((unused)) = true;
std::srand(137);
std::unordered_set<std::string> set;
std::string s;
const unsigned long N = SAMPLES;
const unsigned long k = N/100;
const unsigned int len = 25;
while (set.size() < N)
{
s.clear();
for (unsigned int i = 0; i < len; ++i)
s.push_back(rand() % 128);
set.insert(s);
}
double chi2 = chi2_hash(set, k);
VERIFY( chi2 < k*1.1 );
}
int
main()
{
test_uniform_random();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Use smaller statistics when running on simulators, so it takes less time.
// { dg-options "-std=gnu++0x -DSAMPLES=30000" { target simulator } }
// Copyright (C) 2010-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
......@@ -77,142 +72,3 @@ template <typename Container>
}
return sum/z;
}
// Tests chi^2 for a distribution of uniformly generated random strings.
void
test_uniform_random()
{
bool test __attribute__((unused)) = true;
std::srand(137);
std::unordered_set<std::string> set;
std::string s;
const unsigned long N = SAMPLES;
const unsigned long k = N/100;
const unsigned int len = 25;
while (set.size() < N)
{
s.clear();
for (unsigned int i = 0; i < len; ++i)
s.push_back(rand() % 128);
set.insert(s);
}
double chi2 = chi2_hash(set, k);
VERIFY( chi2 < k*1.1 );
}
// Tests chi^2 for a distribution of strings that differ from each
// other by only a few bits. We start with an arbitrary base string, and
// flip three random bits for each member of the set.
void
test_bit_flip_set()
{
bool test __attribute__((unused)) = true;
const unsigned long N = SAMPLES;
const unsigned long k = N/100;
const unsigned int len = 67;
const unsigned int bitlen = len * 8;
const unsigned int bits_to_flip = 3;
const char base[len+1] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789!@#$%";
std::unordered_set<std::string> set;
while (set.size() < N)
{
std::string s(base, base+len);
for (unsigned int i = 0; i < bits_to_flip; ++i)
{
int bit = rand() % bitlen;
s[bit/8] ^= (1 << (bit%8));
}
set.insert(s);
}
double chi2 = chi2_hash(set, k);
VERIFY( chi2 < k*1.1 );
}
// Tests chi^2 of a set of strings that all have a similar pattern,
// intended to mimic some sort of ID string.
void
test_numeric_pattern_set()
{
bool test __attribute__((unused)) = true;
const unsigned long N = SAMPLES;
const unsigned long k = N/100;
std::vector<std::string> set;
for (unsigned long i = 0; i < N; ++i)
{
long i1 = i % 100000;
long i2 = i / 100000;
char buf[16];
std::sprintf(buf, "XX-%05lu-%05lu", i1, i2);
set.push_back(buf);
}
double chi2 = chi2_hash(set, k);
VERIFY( chi2 < k*1.1 );
}
// Tests chi^2 for a set of strings that all consist of '1' and '0'.
void
test_bit_string_set()
{
bool test __attribute__((unused)) = true;
const unsigned long N = SAMPLES;
const unsigned long k = N/100;
std::vector<std::string> set;
std::string s;
for (unsigned long i = 0; i < N; ++i)
{
s.clear();
for (unsigned int j = 0; j < sizeof(unsigned long) * 8; ++j)
{
const bool bit = (1UL << j) & i;
s.push_back(bit ? '1' : '0');
}
set.push_back(s);
}
double chi2 = chi2_hash(set, k);
VERIFY( chi2 < k*1.1 );
}
// Tests chi^2 for a set of words taken from a document written in English.
void
test_document_words()
{
// That file is 187587 single-word lines. To avoid a timeout, just skip
// this part, which would take up to 95% of the program runtime (with
// SAMPLES == 10000), if we're not supposed to run anywhere that long.
#if SAMPLES >= 100000
bool test __attribute__((unused)) = true;
const std::string f_name = "thirty_years_among_the_dead_preproc.txt";
std::ifstream in(f_name);
VERIFY( in.is_open() );
std::vector<std::string> words;
words.assign(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>());
VERIFY( words.size() > 100000 );
std::sort(words.begin(), words.end());
auto it = std::unique(words.begin(), words.end());
words.erase(it, words.end());
VERIFY( words.size() > 5000 );
const unsigned long k = words.size() / 20;
double chi2 = chi2_hash(words, k);
VERIFY( chi2 < k*1.1 );
#endif
}
int
main()
{
test_uniform_random();
test_bit_flip_set();
test_numeric_pattern_set();
test_bit_string_set();
test_document_words();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment