source: mainline/uspace/lib/c/test/str.c@ dd7df1c

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

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 * Copyright (c) 2015 Michal Koutny
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 <stdio.h>
30#include <str.h>
31#include <pcut/pcut.h>
32
33#define BUFFER_SIZE 256
34
35#define SET_BUFFER(str) snprintf(buffer, BUFFER_SIZE, "%s", str)
36#define EQ(expected, value) PCUT_ASSERT_STR_EQUALS(expected, value)
37
38PCUT_INIT;
39
40PCUT_TEST_SUITE(str);
41
42static char buffer[BUFFER_SIZE];
43
44PCUT_TEST_BEFORE
45{
46 memset(buffer, 0, BUFFER_SIZE);
47}
48
49PCUT_TEST(rtrim)
50{
51 SET_BUFFER("foobar");
52 str_rtrim(buffer, ' ');
53 EQ("foobar", buffer);
54
55 SET_BUFFER(" foobar ");
56 str_rtrim(buffer, ' ');
57 EQ(" foobar", buffer);
58
59 SET_BUFFER(" ššš ");
60 str_rtrim(buffer, ' ');
61 EQ(" ššš", buffer);
62
63 SET_BUFFER("ššAAAšš");
64 str_rtrim(buffer, L'š');
65 EQ("ššAAA", buffer);
66}
67
68PCUT_TEST(ltrim)
69{
70 SET_BUFFER("foobar");
71 str_ltrim(buffer, ' ');
72 EQ("foobar", buffer);
73
74 SET_BUFFER(" foobar ");
75 str_ltrim(buffer, ' ');
76 EQ("foobar ", buffer);
77
78 SET_BUFFER(" ššš ");
79 str_ltrim(buffer, ' ');
80 EQ("ššš ", buffer);
81
82 SET_BUFFER("ššAAAšš");
83 str_ltrim(buffer, L'š');
84 EQ("AAAšš", buffer);
85}
86
87PCUT_TEST(str_str_found)
88{
89 const char *hs = "abracadabra";
90 const char *n = "raca";
91 char *p;
92
93 p = str_str(hs, n);
94 PCUT_ASSERT_TRUE((const char *)p == hs + 2);
95}
96
97PCUT_TEST(str_str_not_found)
98{
99 const char *hs = "abracadabra";
100 const char *n = "racab";
101 char *p;
102
103 p = str_str(hs, n);
104 PCUT_ASSERT_TRUE(p == NULL);
105}
106
107PCUT_TEST(str_str_empty_n)
108{
109 const char *hs = "abracadabra";
110 const char *n = "";
111 char *p;
112
113 p = str_str(hs, n);
114 PCUT_ASSERT_TRUE((const char *)p == hs);
115}
116
117PCUT_EXPORT(str);
Note: See TracBrowser for help on using the repository browser.