|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/ostest/libc_memmem.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <nuttx/config.h> |
| 26 | +#include <assert.h> |
| 27 | +#include <string.h> |
| 28 | + |
| 29 | +#include "ostest.h" |
| 30 | + |
| 31 | +/**************************************************************************** |
| 32 | + * Public Functions |
| 33 | + ****************************************************************************/ |
| 34 | + |
| 35 | +int memmem_test(void) |
| 36 | +{ |
| 37 | + char *haystack = "hello"; |
| 38 | + char *s; |
| 39 | + |
| 40 | + s = memmem(haystack, 5, "hel", 3); |
| 41 | + ASSERT(s == haystack); |
| 42 | + |
| 43 | + s = memmem(haystack, 5, "lo", 2); |
| 44 | + ASSERT(s == haystack + 3); |
| 45 | + |
| 46 | + s = memmem(haystack, 5, "hello", 5); |
| 47 | + ASSERT(s == haystack); |
| 48 | + |
| 49 | + /* Compare '\0' bytes at string ends. */ |
| 50 | + |
| 51 | + s = memmem(haystack, 6, "o", 2); |
| 52 | + ASSERT(s == haystack + 4); |
| 53 | + |
| 54 | + /* Must not find needle that is right after end of haystack. */ |
| 55 | + |
| 56 | + s = memmem("helloX", 5, "X", 1); |
| 57 | + ASSERT(s == NULL); |
| 58 | + |
| 59 | + /* Too long needle should return NULL. */ |
| 60 | + |
| 61 | + s = memmem(haystack, 5, "hellohello", 10); |
| 62 | + ASSERT(s == NULL); |
| 63 | + |
| 64 | + /* Zero length needle is deemed to reside at start of haystack. */ |
| 65 | + |
| 66 | + s = memmem(haystack, 5, "", 0); |
| 67 | + ASSERT(s == haystack); |
| 68 | + |
| 69 | + return OK; |
| 70 | +} |
0 commit comments