avstring.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdio.h>
  19. #include "libavutil/common.h"
  20. #include "libavutil/mem.h"
  21. #include "libavutil/avstring.h"
  22. int main(void)
  23. {
  24. int i;
  25. char *fullpath, *ptr;
  26. static const char * const strings[] = {
  27. "''",
  28. "",
  29. ":",
  30. "\\",
  31. "'",
  32. " '' :",
  33. " '' '' :",
  34. "foo '' :",
  35. "'foo'",
  36. "foo ",
  37. " ' foo ' ",
  38. "foo\\",
  39. "foo': blah:blah",
  40. "foo\\: blah:blah",
  41. "foo\'",
  42. "'foo : ' :blahblah",
  43. "\\ :blah",
  44. " foo",
  45. " foo ",
  46. " foo \\ ",
  47. "foo ':blah",
  48. " foo bar : blahblah",
  49. "\\f\\o\\o",
  50. "'foo : \\ \\ ' : blahblah",
  51. "'\\fo\\o:': blahblah",
  52. "\\'fo\\o\\:': foo ' :blahblah"
  53. };
  54. const char *haystack = "Education consists mainly in what we have unlearned.";
  55. const char * const needle[] = {"learned.", "unlearned.", "Unlearned"};
  56. printf("Testing av_get_token()\n");
  57. for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
  58. const char *p = strings[i];
  59. char *q;
  60. printf("|%s|", p);
  61. q = av_get_token(&p, ":");
  62. printf(" -> |%s|", q);
  63. printf(" + |%s|\n", p);
  64. av_free(q);
  65. }
  66. printf("Testing av_append_path_component()\n");
  67. #define TEST_APPEND_PATH_COMPONENT(path, component, expected) \
  68. fullpath = av_append_path_component((path), (component)); \
  69. printf("%s = %s\n", fullpath ? fullpath : "(null)", expected); \
  70. av_free(fullpath);
  71. TEST_APPEND_PATH_COMPONENT(NULL, NULL, "(null)")
  72. TEST_APPEND_PATH_COMPONENT("path", NULL, "path");
  73. TEST_APPEND_PATH_COMPONENT(NULL, "comp", "comp");
  74. TEST_APPEND_PATH_COMPONENT("path", "comp", "path/comp");
  75. TEST_APPEND_PATH_COMPONENT("path/", "comp", "path/comp");
  76. TEST_APPEND_PATH_COMPONENT("path", "/comp", "path/comp");
  77. TEST_APPEND_PATH_COMPONENT("path/", "/comp", "path/comp");
  78. TEST_APPEND_PATH_COMPONENT("path/path2/", "/comp/comp2", "path/path2/comp/comp2");
  79. /*Testing av_strnstr()*/
  80. #define TEST_STRNSTR(haystack, needle, hay_length, expected) \
  81. ptr = av_strnstr(haystack, needle, hay_length); \
  82. if (ptr != expected){ \
  83. printf("expected: %p, received %p\n", expected, ptr); \
  84. }
  85. TEST_STRNSTR(haystack, needle [0], strlen(haystack), haystack+44);
  86. TEST_STRNSTR(haystack, needle [1], strlen(haystack), haystack+42);
  87. TEST_STRNSTR(haystack, needle [2], strlen(haystack), NULL );
  88. TEST_STRNSTR(haystack, strings[1], strlen(haystack), haystack );
  89. /*Testing av_strireplace()*/
  90. #define TEST_STRIREPLACE(haystack, needle, expected) \
  91. ptr = av_strireplace(haystack, needle, "instead"); \
  92. if (ptr == NULL) { \
  93. printf("error, received null pointer!\n"); \
  94. } else { \
  95. if (strcmp(ptr, expected) != 0) \
  96. printf( "expected: %s, received: %s\n", expected, ptr); \
  97. av_free(ptr); \
  98. }
  99. TEST_STRIREPLACE(haystack, needle [0], "Education consists mainly in what we have uninstead");
  100. TEST_STRIREPLACE(haystack, needle [1], "Education consists mainly in what we have instead");
  101. TEST_STRIREPLACE(haystack, needle [2], "Education consists mainly in what we have instead.");
  102. TEST_STRIREPLACE(haystack, needle [1], "Education consists mainly in what we have instead");
  103. /*Testing av_d2str()*/
  104. #define TEST_D2STR(value, expected) \
  105. if((ptr = av_d2str(value)) == NULL){ \
  106. printf("error, received null pointer!\n"); \
  107. } else { \
  108. if(strcmp(ptr, expected) != 0) \
  109. printf( "expected: %s, received: %s\n", expected, ptr); \
  110. av_free(ptr); \
  111. }
  112. TEST_D2STR(0 , "0.000000");
  113. TEST_D2STR(-1.2333234, "-1.233323");
  114. TEST_D2STR(-1.2333237, "-1.233324");
  115. return 0;
  116. }