basename.phpt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. --TEST--
  2. basename
  3. --CREDITS--
  4. Dave Kelsey <d_kelsey@uk.ibm.com>
  5. --FILE--
  6. <?php
  7. /*
  8. * Function is implemented in ext/standard/string.c
  9. */
  10. $file_paths = array (
  11. /* simple paths */
  12. array("bar"),
  13. array("/foo/bar"),
  14. array("foo/bar"),
  15. array("/bar"),
  16. /* simple paths with trailing slashes */
  17. array("bar/"),
  18. array("/bar/"),
  19. array("/foo/bar/"),
  20. array("foo/bar/"),
  21. array("/bar/"),
  22. /* paths with suffix removal */
  23. array("bar.gz", ".gz"),
  24. array("bar.gz", "bar.gz"),
  25. array("/foo/bar.gz", ".gz"),
  26. array("foo/bar.gz", ".gz"),
  27. array("/bar.gz", ".gz"),
  28. /* paths with suffix and trailing slashes with suffix removal*/
  29. array("bar.gz/", ".gz"),
  30. array("/bar.gz/", ".gz"),
  31. array("/foo/bar.gz/", ".gz"),
  32. array("foo/bar.gz/", ".gz"),
  33. array("/bar.gz/", ".gz"),
  34. /* paths with basename only suffix, with suffix removal*/
  35. array("/.gz", ".gz"),
  36. array(".gz", ".gz"),
  37. array("/foo/.gz", ".gz"),
  38. /* paths with basename only suffix & trailing slashes, with suffix removal*/
  39. array(".gz/", ".gz"),
  40. array("/foo/.gz/", ".gz"),
  41. array("foo/.gz/", ".gz"),
  42. /* paths with binary value to check if the function is binary safe*/
  43. array("foo".chr(0)."bar"),
  44. array("/foo".chr(0)."bar"),
  45. array("/foo".chr(0)."bar/"),
  46. array("foo".chr(0)."bar/"),
  47. array("foo".chr(0)."bar/test"),
  48. array("/foo".chr(0)."bar/bar.gz", ".gz"),
  49. array("/foo".chr(0)."bar/bar.gz")
  50. );
  51. $file_path_variations = array (
  52. /* paths with shortcut home dir char, with suffix variation */
  53. array("~/home/user/bar"),
  54. array("~/home/user/bar", ""),
  55. array("~/home/user/bar", ' '),
  56. array("~/home/user/bar.tar", ".tar"),
  57. array("~/home/user/bar.tar", "~"),
  58. array("~/home/user/bar.tar/", "~"),
  59. array("~/home/user/bar.tar/", ""),
  60. array("~/home/user/bar.tar", ''),
  61. array("~/home/user/bar.tar", " "),
  62. /* paths with hostname:dir notation, with suffix variation */
  63. array("hostname:/home/usr/bar.tar"),
  64. array("hostname:/home/user/bar.tar", "home"),
  65. array("hostname:/home/user/tbar.gz", "bar.gz"),
  66. array("hostname:/home/user/tbar.gz", "/bar.gz"),
  67. array("hostname:/home/user/tbar.gz", "/bar.gz/"),
  68. array("hostname:/home/user/tbar.gz/", "/bar.gz/"),
  69. array("hostname:/home/user/tbar.gz/", "/bar.gz/"),
  70. array("hostname:/home/user/My Pics.gz/", "/bar.gz/"),
  71. array("hostname:/home/user/My Pics.gz/", "Pics.gz/"),
  72. array("hostname:/home/user/My Pics.gz/", "Pics.gz"),
  73. array("hostname:/home/user/My Pics.gz/", ".gz"),
  74. array("hostname:/home/user/My Pics.gz/"),
  75. array("hostname:/home/user/My Pics.gz/", ' '),
  76. array("hostname:/home/user/My Pics.gz/", ''),
  77. array("hostname:/home/user/My Pics.gz/", "My Pics.gz"),
  78. /* paths with numeirc strings */
  79. array("10.5"),
  80. array("10.5", ".5"),
  81. array("10.5", "10.5"),
  82. array("10"),
  83. array("105", "5"),
  84. array("/10.5"),
  85. array("10.5/"),
  86. array("10/10.gz"),
  87. array("0"),
  88. array('0'),
  89. /* paths and suffix given as same */
  90. array("bar.gz", "bar.gz"),
  91. array("/bar.gz", "/bar.gz"),
  92. array("/bar.gz/", "/bar.gz/"),
  93. array(" ", " "),
  94. array(' ', ' '),
  95. /* path with spaces */
  96. array(" "),
  97. array(' '),
  98. /* empty paths */
  99. array(""),
  100. array(''),
  101. );
  102. function check_basename( $path_arrays ) {
  103. $loop_counter = 1;
  104. foreach ($path_arrays as $path) {
  105. echo "\n--Iteration $loop_counter--\n"; $loop_counter++;
  106. if( 1 == count($path) ) { // no suffix provided
  107. var_dump( basename($path[0]) );
  108. } else { // path as well as suffix provided,
  109. var_dump( basename($path[0], $path[1]) );
  110. }
  111. }
  112. }
  113. echo "*** Testing basic operations ***\n";
  114. check_basename( $file_paths );
  115. echo "\n*** Testing possible variations in path and suffix ***\n";
  116. check_basename( $file_path_variations );
  117. echo "Done\n";
  118. ?>
  119. --EXPECTF--
  120. *** Testing basic operations ***
  121. --Iteration 1--
  122. string(3) "bar"
  123. --Iteration 2--
  124. string(3) "bar"
  125. --Iteration 3--
  126. string(3) "bar"
  127. --Iteration 4--
  128. string(3) "bar"
  129. --Iteration 5--
  130. string(3) "bar"
  131. --Iteration 6--
  132. string(3) "bar"
  133. --Iteration 7--
  134. string(3) "bar"
  135. --Iteration 8--
  136. string(3) "bar"
  137. --Iteration 9--
  138. string(3) "bar"
  139. --Iteration 10--
  140. string(3) "bar"
  141. --Iteration 11--
  142. string(6) "bar.gz"
  143. --Iteration 12--
  144. string(3) "bar"
  145. --Iteration 13--
  146. string(3) "bar"
  147. --Iteration 14--
  148. string(3) "bar"
  149. --Iteration 15--
  150. string(3) "bar"
  151. --Iteration 16--
  152. string(3) "bar"
  153. --Iteration 17--
  154. string(3) "bar"
  155. --Iteration 18--
  156. string(3) "bar"
  157. --Iteration 19--
  158. string(3) "bar"
  159. --Iteration 20--
  160. string(3) ".gz"
  161. --Iteration 21--
  162. string(3) ".gz"
  163. --Iteration 22--
  164. string(3) ".gz"
  165. --Iteration 23--
  166. string(3) ".gz"
  167. --Iteration 24--
  168. string(3) ".gz"
  169. --Iteration 25--
  170. string(3) ".gz"
  171. --Iteration 26--
  172. string(7) "foo%0bar"
  173. --Iteration 27--
  174. string(7) "foo%0bar"
  175. --Iteration 28--
  176. string(7) "foo%0bar"
  177. --Iteration 29--
  178. string(7) "foo%0bar"
  179. --Iteration 30--
  180. string(4) "test"
  181. --Iteration 31--
  182. string(3) "bar"
  183. --Iteration 32--
  184. string(6) "bar.gz"
  185. *** Testing possible variations in path and suffix ***
  186. --Iteration 1--
  187. string(3) "bar"
  188. --Iteration 2--
  189. string(3) "bar"
  190. --Iteration 3--
  191. string(3) "bar"
  192. --Iteration 4--
  193. string(3) "bar"
  194. --Iteration 5--
  195. string(7) "bar.tar"
  196. --Iteration 6--
  197. string(7) "bar.tar"
  198. --Iteration 7--
  199. string(7) "bar.tar"
  200. --Iteration 8--
  201. string(7) "bar.tar"
  202. --Iteration 9--
  203. string(7) "bar.tar"
  204. --Iteration 10--
  205. string(7) "bar.tar"
  206. --Iteration 11--
  207. string(7) "bar.tar"
  208. --Iteration 12--
  209. string(1) "t"
  210. --Iteration 13--
  211. string(7) "tbar.gz"
  212. --Iteration 14--
  213. string(7) "tbar.gz"
  214. --Iteration 15--
  215. string(7) "tbar.gz"
  216. --Iteration 16--
  217. string(7) "tbar.gz"
  218. --Iteration 17--
  219. string(10) "My Pics.gz"
  220. --Iteration 18--
  221. string(10) "My Pics.gz"
  222. --Iteration 19--
  223. string(3) "My "
  224. --Iteration 20--
  225. string(7) "My Pics"
  226. --Iteration 21--
  227. string(10) "My Pics.gz"
  228. --Iteration 22--
  229. string(10) "My Pics.gz"
  230. --Iteration 23--
  231. string(10) "My Pics.gz"
  232. --Iteration 24--
  233. string(10) "My Pics.gz"
  234. --Iteration 25--
  235. string(4) "10.5"
  236. --Iteration 26--
  237. string(2) "10"
  238. --Iteration 27--
  239. string(4) "10.5"
  240. --Iteration 28--
  241. string(2) "10"
  242. --Iteration 29--
  243. string(2) "10"
  244. --Iteration 30--
  245. string(4) "10.5"
  246. --Iteration 31--
  247. string(4) "10.5"
  248. --Iteration 32--
  249. string(5) "10.gz"
  250. --Iteration 33--
  251. string(1) "0"
  252. --Iteration 34--
  253. string(1) "0"
  254. --Iteration 35--
  255. string(6) "bar.gz"
  256. --Iteration 36--
  257. string(6) "bar.gz"
  258. --Iteration 37--
  259. string(6) "bar.gz"
  260. --Iteration 38--
  261. string(1) " "
  262. --Iteration 39--
  263. string(1) " "
  264. --Iteration 40--
  265. string(1) " "
  266. --Iteration 41--
  267. string(1) " "
  268. --Iteration 42--
  269. string(0) ""
  270. --Iteration 43--
  271. string(0) ""
  272. Done