strutil_test.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <config.h>
  2. #include <apt-pkg/strutl.h>
  3. #include <string>
  4. #include <vector>
  5. #include "assert.h"
  6. int main()
  7. {
  8. std::string input, output, expected;
  9. // no input
  10. input = "foobar";
  11. expected = "foobar";
  12. output = DeEscapeString(input);
  13. equals(output, expected);
  14. // hex and octal
  15. input = "foo\\040bar\\x0abaz";
  16. expected = "foo bar\nbaz";
  17. output = DeEscapeString(input);
  18. equals(output, expected);
  19. // at the end
  20. input = "foo\\040";
  21. expected = "foo ";
  22. output = DeEscapeString(input);
  23. equals(output, expected);
  24. // double escape
  25. input = "foo\\\\ x";
  26. expected = "foo\\ x";
  27. output = DeEscapeString(input);
  28. equals(output, expected);
  29. // double escape at the end
  30. input = "\\\\foo\\\\";
  31. expected = "\\foo\\";
  32. output = DeEscapeString(input);
  33. equals(output, expected);
  34. // the string that we actually need it for
  35. input = "/media/Ubuntu\\04011.04\\040amd64";
  36. expected = "/media/Ubuntu 11.04 amd64";
  37. output = DeEscapeString(input);
  38. equals(output, expected);
  39. // Split
  40. input = "status: libnet1:amd64: unpacked";
  41. std::vector<std::string> result = StringSplit(input, ": ");
  42. equals(result[0], "status");
  43. equals(result[1], "libnet1:amd64");
  44. equals(result[2], "unpacked");
  45. equals(result.size(), 3);
  46. input = "status: libnet1:amd64: unpacked";
  47. result = StringSplit(input, "xxx");
  48. equals(result[0], input);
  49. equals(result.size(), 1);
  50. input = "status: libnet1:amd64: unpacked";
  51. result = StringSplit(input, "");
  52. equals(result.size(), 0);
  53. input = "x:y:z";
  54. result = StringSplit(input, ":", 2);
  55. equals(result.size(), 2);
  56. equals(result[0], "x");
  57. equals(result[1], "y:z");
  58. input = "abc";
  59. result = StringSplit(input, "");
  60. equals(result.size(), 0);
  61. // endswith
  62. bool b;
  63. input = "abcd";
  64. b = APT::String::Endswith(input, "d");
  65. equals(b, true);
  66. b = APT::String::Endswith(input, "cd");
  67. equals(b, true);
  68. b = APT::String::Endswith(input, "abcd");
  69. equals(b, true);
  70. b = APT::String::Endswith(input, "x");
  71. equals(b, false);
  72. b = APT::String::Endswith(input, "abcndefg");
  73. equals(b, false);
  74. return 0;
  75. }