You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glibcxx_sanity.cpp 1.7KB

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