strutil_test.cc 2.1 KB

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