unicode.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <sstream>
  2. #include <iostream>
  3. #include "unicode.hpp"
  4. #include "streamConsumer.hh"
  5. #define FAILED(got, op, expt) {std::cout << __FILE__ << ":" << __LINE__ << ": failed asserting " << got << " " << op << " expected " << expt << std::endl; return false; }
  6. class StreamConsumerTester: public StreamConsumer
  7. {
  8. public:
  9. static const std::string getStringFromUnicode(const char unicode[4])
  10. {
  11. std::stringstream ss;
  12. appendUnicode(unicode, ss);
  13. return ss.str();
  14. };
  15. static bool test()
  16. {
  17. std::string s = getStringFromUnicode("00e8");
  18. if (s != "è")
  19. FAILED((int)(s.c_str()[0]), "!=", (int)L'è');
  20. return true;
  21. };
  22. };
  23. bool simpleTest()
  24. {
  25. if (hexbyte<unsigned short>("0020", 4) != 32)
  26. FAILED(hexbyte<unsigned short>("0020", 4), "!=", 32);
  27. if (hexbyte<unsigned short>("20", 2) != 32)
  28. FAILED(hexbyte<unsigned short>("2020", 4), "!=", 32);
  29. if (hexbyte<unsigned short>("2020", 4) != 8224)
  30. FAILED(hexbyte<unsigned short>("2020", 4), "!=", 8224);
  31. if (hexbyte<unsigned short>("FFFF", 4) != 65535)
  32. FAILED(hexbyte<unsigned short>("FFFF", 4), "!=", 65535);
  33. if (hexbyte<unsigned short>("0000", 4) != 0)
  34. FAILED(hexbyte<unsigned short>("0000", 4), "!=", 0);
  35. if (hexbyte<unsigned short>("", 0) != 0)
  36. FAILED(hexbyte<unsigned short>("", 0), "!=", 0);
  37. if (hexbyte<unsigned short>("002020", 6) != 8224)
  38. FAILED(hexbyte<unsigned short>("2020", 6), "!=", 8224);
  39. return true;
  40. }
  41. int main()
  42. {
  43. if (!simpleTest())
  44. exit(EXIT_FAILURE);
  45. if (!StreamConsumerTester::test())
  46. exit(EXIT_FAILURE);
  47. exit(EXIT_SUCCESS);
  48. }