aboutsummaryrefslogtreecommitdiff
path: root/src/compat/glibcxx_sanity.cpp
diff options
context:
space:
mode:
authorCory Fields <cory-nospam-@coryfields.com>2014-06-13 19:19:35 -0400
committerCory Fields <cory-nospam-@coryfields.com>2014-06-16 17:21:34 -0400
commit679240d0e92f71b38cefd23e3d99e8ebf63c2508 (patch)
tree31b055adc3fcd07bb6b58903a590165de90cacce /src/compat/glibcxx_sanity.cpp
parent11404af34c5e3d5aa334e1cfd8edee82fb9dc9a4 (diff)
downloadbitcoin-679240d0e92f71b38cefd23e3d99e8ebf63c2508.tar.xz
sanity: add libc/stdlib sanity checks
These are meant to test our back-compat stubs, but they are enabled for all builds for the sake of consistency.
Diffstat (limited to 'src/compat/glibcxx_sanity.cpp')
-rw-r--r--src/compat/glibcxx_sanity.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/compat/glibcxx_sanity.cpp b/src/compat/glibcxx_sanity.cpp
new file mode 100644
index 0000000000..2ff70948fd
--- /dev/null
+++ b/src/compat/glibcxx_sanity.cpp
@@ -0,0 +1,61 @@
+#include <locale>
+#include <list>
+#include <stdexcept>
+
+namespace{
+
+// trigger: use ctype<char>::widen to trigger ctype<char>::_M_widen_init().
+// test: convert a char from narrow to wide and back. Verify that the result
+// matches the original.
+bool sanity_test_widen(char testchar)
+{
+ const std::ctype<char>& test(std::use_facet< std::ctype<char> >(std::locale()));
+ return test.narrow(test.widen(testchar),'b') == testchar;
+}
+
+// trigger: use list::push_back and list::pop_back to trigger _M_hook and
+// _M_unhook.
+// test: Push a sequence of integers into a list. Pop them off and verify that
+// they match the original sequence.
+bool sanity_test_list(unsigned int size)
+{
+ std::list<unsigned int> test;
+ for (unsigned int i = 0; i != size; ++i)
+ test.push_back(i+1);
+
+ if (test.size() != size)
+ return false;
+
+ while (!test.empty())
+ {
+ if(test.back() != test.size())
+ return false;
+ test.pop_back();
+ }
+ return true;
+}
+
+} // anon namespace
+
+// trigger: string::at(x) on an empty string to trigger __throw_out_of_range_fmt.
+// test: force std::string to throw an out_of_range exception. Verify that
+// it's caught correctly.
+bool sanity_test_range_fmt()
+{
+ std::string test;
+ try
+ {
+ test.at(1);
+ }
+ catch (const std::out_of_range&)
+ {
+ return true;
+ }
+ catch (...){}
+ return false;
+}
+
+bool glibcxx_sanity_test()
+{
+ return sanity_test_widen('a') && sanity_test_list(100) && sanity_test_range_fmt();
+}