teststrmatch.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright 2002-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "testutil.h"
  17. #include "apr.h"
  18. #include "apr_general.h"
  19. #include "apr_strmatch.h"
  20. #if APR_HAVE_STDLIB_H
  21. #include <stdlib.h>
  22. #endif
  23. #define APR_WANT_STDIO
  24. #define APR_WANT_STRFUNC
  25. #include "apr_want.h"
  26. static void test_str(abts_case *tc, void *data)
  27. {
  28. apr_pool_t *pool = p;
  29. const apr_strmatch_pattern *pattern;
  30. const apr_strmatch_pattern *pattern_nocase;
  31. const apr_strmatch_pattern *pattern_onechar;
  32. const apr_strmatch_pattern *pattern_zero;
  33. const char *match = NULL;
  34. const char *input1 = "string that contains a patterN...";
  35. const char *input2 = "string that contains a pattern...";
  36. const char *input3 = "pattern at the start of a string";
  37. const char *input4 = "string that ends with a pattern";
  38. const char *input5 = "patter\200n not found, negative chars in input";
  39. const char *input6 = "patter\200n, negative chars, contains pattern...";
  40. pattern = apr_strmatch_precompile(pool, "pattern", 1);
  41. ABTS_PTR_NOTNULL(tc, pattern);
  42. pattern_nocase = apr_strmatch_precompile(pool, "pattern", 0);
  43. ABTS_PTR_NOTNULL(tc, pattern_nocase);
  44. pattern_onechar = apr_strmatch_precompile(pool, "g", 0);
  45. ABTS_PTR_NOTNULL(tc, pattern_onechar);
  46. pattern_zero = apr_strmatch_precompile(pool, "", 0);
  47. ABTS_PTR_NOTNULL(tc, pattern_zero);
  48. match = apr_strmatch(pattern, input1, strlen(input1));
  49. ABTS_PTR_EQUAL(tc, match, NULL);
  50. match = apr_strmatch(pattern, input2, strlen(input2));
  51. ABTS_PTR_EQUAL(tc, match, input2 + 23);
  52. match = apr_strmatch(pattern_onechar, input1, strlen(input1));
  53. ABTS_PTR_EQUAL(tc, match, input1 + 5);
  54. match = apr_strmatch(pattern_zero, input1, strlen(input1));
  55. ABTS_PTR_EQUAL(tc, match, input1);
  56. match = apr_strmatch(pattern_nocase, input1, strlen(input1));
  57. ABTS_PTR_EQUAL(tc, match, input1 + 23);
  58. match = apr_strmatch(pattern, input3, strlen(input3));
  59. ABTS_PTR_EQUAL(tc, match, input3);
  60. match = apr_strmatch(pattern, input4, strlen(input4));
  61. ABTS_PTR_EQUAL(tc, match, input4 + 24);
  62. match = apr_strmatch(pattern, input5, strlen(input5));
  63. ABTS_PTR_EQUAL(tc, match, NULL);
  64. match = apr_strmatch(pattern, input6, strlen(input6));
  65. ABTS_PTR_EQUAL(tc, match, input6 + 35);
  66. }
  67. abts_suite *teststrmatch(abts_suite *suite)
  68. {
  69. suite = ADD_SUITE(suite);
  70. abts_run_test(suite, test_str, NULL);
  71. return suite;
  72. }