source: mainline/uspace/lib/c/test/mem.c@ 8080262

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8080262 was 2498b95, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Move memchr to libc and add tests for other memxxx functions.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <mem.h>
30#include <pcut/pcut.h>
31
32PCUT_INIT;
33
34PCUT_TEST_SUITE(mem);
35
36/** memcpy function */
37PCUT_TEST(memcpy)
38{
39 char buf[5];
40 void *p;
41
42 p = memcpy(buf, "abc\0d", 5);
43 PCUT_ASSERT_TRUE(p == buf);
44
45 PCUT_ASSERT_INT_EQUALS('a', buf[0]);
46 PCUT_ASSERT_INT_EQUALS('b', buf[1]);
47 PCUT_ASSERT_INT_EQUALS('c', buf[2]);
48 PCUT_ASSERT_INT_EQUALS('\0', buf[3]);
49 PCUT_ASSERT_INT_EQUALS('d', buf[4]);
50}
51
52/** memmove function */
53PCUT_TEST(memmove)
54{
55 char buf[] = "abc\0d";
56 void *p;
57
58 p = memmove(buf, buf + 1, 4);
59 PCUT_ASSERT_TRUE(p == buf);
60
61 PCUT_ASSERT_INT_EQUALS('b', buf[0]);
62 PCUT_ASSERT_INT_EQUALS('c', buf[1]);
63 PCUT_ASSERT_INT_EQUALS('\0', buf[2]);
64 PCUT_ASSERT_INT_EQUALS('d', buf[3]);
65 PCUT_ASSERT_INT_EQUALS('d', buf[4]);
66}
67
68/** memcmp function */
69PCUT_TEST(memcmp)
70{
71 const char *s1 = "ab" "\0" "1d";
72 const char *s2 = "ab" "\0" "2d";
73 int c;
74
75 c = memcmp(s1, s2, 3);
76 PCUT_ASSERT_INT_EQUALS(0, c);
77
78 c = memcmp(s1, s2, 4);
79 PCUT_ASSERT_TRUE(c < 0);
80
81 c = memcmp(s2, s1, 4);
82 PCUT_ASSERT_TRUE(c > 0);
83}
84
85/** memchr function */
86PCUT_TEST(memchr)
87{
88 const char *s = "abc\0d";
89 void *p;
90
91 p = memchr(s, 'd', 5);
92 PCUT_ASSERT_TRUE(p == s + 4);
93
94 p = memchr(s, '\0', 5);
95 PCUT_ASSERT_TRUE(p == s + 3);
96
97 p = memchr(s, 'd', 4);
98 PCUT_ASSERT_TRUE(p == NULL);
99}
100
101/** memset function */
102PCUT_TEST(memset)
103{
104 char buf[5];
105 void *p;
106
107 buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = 'a';
108 p = memset(buf, 'x', 5);
109 PCUT_ASSERT_TRUE(p == buf);
110
111 PCUT_ASSERT_INT_EQUALS('x', buf[0]);
112 PCUT_ASSERT_INT_EQUALS('x', buf[1]);
113 PCUT_ASSERT_INT_EQUALS('x', buf[2]);
114 PCUT_ASSERT_INT_EQUALS('x', buf[3]);
115 PCUT_ASSERT_INT_EQUALS('x', buf[4]);
116}
117
118PCUT_EXPORT(mem);
Note: See TracBrowser for help on using the repository browser.