Skip to content

Commit ec4d7a1

Browse files
juniskanexiaoxiang781216
authored andcommitted
ostest: add test for libc memmem() function
Signed-off-by: Juha Niskanen <[email protected]>
1 parent 5519de8 commit ec4d7a1

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

testing/ostest/CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020

2121
if(CONFIG_TESTING_OSTEST)
2222

23-
set(SRCS getopt.c restart.c sigprocmask.c sighand.c signest.c sighelper.c)
23+
set(SRCS
24+
getopt.c
25+
libc_memmem.c
26+
restart.c
27+
sigprocmask.c
28+
sighand.c
29+
signest.c
30+
sighelper.c)
2431

2532
if(CONFIG_DEV_NULL)
2633
list(APPEND SRCS dev_null.c)

testing/ostest/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ MODULE = $(CONFIG_TESTING_OSTEST)
2929

3030
# NuttX OS Test
3131

32-
CSRCS = getopt.c restart.c sigprocmask.c sighand.c signest.c
33-
CSRCS += sighelper.c
32+
CSRCS = getopt.c libc_memmem.c restart.c sigprocmask.c sighand.c \
33+
signest.c sighelper.c
34+
3435
MAINSRC = ostest_main.c
3536

3637
ifeq ($(CONFIG_DEV_NULL),y)

testing/ostest/libc_memmem.c

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

testing/ostest/ostest.h

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191

9292
int getopt_test(void);
9393

94+
/* libc_memmem.c ************************************************************/
95+
96+
int memmem_test(void);
97+
9498
/* setvbuf.c ****************************************************************/
9599

96100
#ifndef CONFIG_STDIO_DISABLE_BUFFERING

testing/ostest/ostest_main.c

+6
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ static int user_main(int argc, char *argv[])
260260
getopt_test();
261261
check_test_memory_usage();
262262

263+
/* Test misc libc functions. */
264+
265+
printf("\nuser_main: libc tests\n");
266+
memmem_test();
267+
check_test_memory_usage();
268+
263269
/* If retention of child status is enable, then suppress it for this task.
264270
* This task may produce many, many children (especially if
265271
* CONFIG_TESTING_OSTEST_LOOPS) and it does not harvest their exit status.

0 commit comments

Comments
 (0)