Bitcoin ABC  0.28.12
P2P Digital Currency
glibcxx_sanity.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <list>
6 #include <locale>
7 #include <stdexcept>
8 
9 namespace {
10 // trigger: use ctype<char>::widen to trigger ctype<char>::_M_widen_init().
11 // test: convert a char from narrow to wide and back. Verify that the result
12 // matches the original.
13 bool sanity_test_widen(char testchar) {
14  const std::ctype<char> &test(
15  std::use_facet<std::ctype<char>>(std::locale()));
16  return test.narrow(test.widen(testchar), 'b') == testchar;
17 }
18 
19 // trigger: use list::push_back and list::pop_back to trigger _M_hook and
20 // _M_unhook.
21 // test: Push a sequence of integers into a list. Pop them off and verify that
22 // they match the original sequence.
23 bool sanity_test_list(unsigned int size) {
24  std::list<unsigned int> test;
25  for (unsigned int i = 0; i != size; ++i) {
26  test.push_back(i + 1);
27  }
28 
29  if (test.size() != size) {
30  return false;
31  }
32 
33  while (!test.empty()) {
34  if (test.back() != test.size()) {
35  return false;
36  }
37  test.pop_back();
38  }
39  return true;
40 }
41 
42 } // namespace
43 
44 // trigger: string::at(x) on an empty string to trigger
45 // __throw_out_of_range_fmt.
46 // test: force std::string to throw an out_of_range exception. Verify that
47 // it's caught correctly.
49  std::string test;
50  try {
51  [[maybe_unused]] auto &c = test.at(1);
52  } catch (const std::out_of_range &) {
53  return true;
54  } catch (...) {
55  }
56  return false;
57 }
58 
60  return sanity_test_widen('a') && sanity_test_list(100) &&
62 }
bool glibcxx_sanity_test()
bool sanity_test_range_fmt()