[a18a8b9] | 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 |
|
---|
[0600976] | 29 | #include "pcut/asserts.h"
|
---|
[a18a8b9] | 30 | #include <stdio.h>
|
---|
| 31 | #include <str.h>
|
---|
| 32 | #include <pcut/pcut.h>
|
---|
| 33 |
|
---|
| 34 | #define BUFFER_SIZE 256
|
---|
| 35 |
|
---|
| 36 | #define SET_BUFFER(str) snprintf(buffer, BUFFER_SIZE, "%s", str)
|
---|
| 37 | #define EQ(expected, value) PCUT_ASSERT_STR_EQUALS(expected, value)
|
---|
| 38 |
|
---|
[3f932a7e] | 39 | PCUT_INIT;
|
---|
[a18a8b9] | 40 |
|
---|
| 41 | PCUT_TEST_SUITE(str);
|
---|
| 42 |
|
---|
| 43 | static char buffer[BUFFER_SIZE];
|
---|
| 44 |
|
---|
[3bacee1] | 45 | PCUT_TEST_BEFORE
|
---|
| 46 | {
|
---|
[a18a8b9] | 47 | memset(buffer, 0, BUFFER_SIZE);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[3bacee1] | 50 | PCUT_TEST(rtrim)
|
---|
| 51 | {
|
---|
[a18a8b9] | 52 | SET_BUFFER("foobar");
|
---|
| 53 | str_rtrim(buffer, ' ');
|
---|
| 54 | EQ("foobar", buffer);
|
---|
| 55 |
|
---|
| 56 | SET_BUFFER(" foobar ");
|
---|
| 57 | str_rtrim(buffer, ' ');
|
---|
| 58 | EQ(" foobar", buffer);
|
---|
| 59 |
|
---|
| 60 | SET_BUFFER(" ššš ");
|
---|
| 61 | str_rtrim(buffer, ' ');
|
---|
| 62 | EQ(" ššš", buffer);
|
---|
| 63 |
|
---|
| 64 | SET_BUFFER("ššAAAšš");
|
---|
| 65 | str_rtrim(buffer, L'š');
|
---|
| 66 | EQ("ššAAA", buffer);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[3bacee1] | 69 | PCUT_TEST(ltrim)
|
---|
| 70 | {
|
---|
[a18a8b9] | 71 | SET_BUFFER("foobar");
|
---|
| 72 | str_ltrim(buffer, ' ');
|
---|
| 73 | EQ("foobar", buffer);
|
---|
| 74 |
|
---|
| 75 | SET_BUFFER(" foobar ");
|
---|
| 76 | str_ltrim(buffer, ' ');
|
---|
| 77 | EQ("foobar ", buffer);
|
---|
| 78 |
|
---|
| 79 | SET_BUFFER(" ššš ");
|
---|
| 80 | str_ltrim(buffer, ' ');
|
---|
| 81 | EQ("ššš ", buffer);
|
---|
| 82 |
|
---|
| 83 | SET_BUFFER("ššAAAšš");
|
---|
| 84 | str_ltrim(buffer, L'š');
|
---|
| 85 | EQ("AAAšš", buffer);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[da680b4b] | 88 | PCUT_TEST(str_str_found)
|
---|
| 89 | {
|
---|
| 90 | const char *hs = "abracadabra";
|
---|
| 91 | const char *n = "raca";
|
---|
| 92 | char *p;
|
---|
| 93 |
|
---|
| 94 | p = str_str(hs, n);
|
---|
| 95 | PCUT_ASSERT_TRUE((const char *)p == hs + 2);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | PCUT_TEST(str_str_not_found)
|
---|
| 99 | {
|
---|
| 100 | const char *hs = "abracadabra";
|
---|
| 101 | const char *n = "racab";
|
---|
| 102 | char *p;
|
---|
| 103 |
|
---|
| 104 | p = str_str(hs, n);
|
---|
| 105 | PCUT_ASSERT_TRUE(p == NULL);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | PCUT_TEST(str_str_empty_n)
|
---|
| 109 | {
|
---|
| 110 | const char *hs = "abracadabra";
|
---|
| 111 | const char *n = "";
|
---|
| 112 | char *p;
|
---|
| 113 |
|
---|
| 114 | p = str_str(hs, n);
|
---|
| 115 | PCUT_ASSERT_TRUE((const char *)p == hs);
|
---|
| 116 | }
|
---|
[a18a8b9] | 117 |
|
---|
[0600976] | 118 | PCUT_TEST(str_non_shortest)
|
---|
| 119 | {
|
---|
| 120 | /* Overlong zero. */
|
---|
| 121 | const char overlong1[] = { 0b11000000, 0b10000000, 0 };
|
---|
| 122 | const char overlong2[] = { 0b11100000, 0b10000000, 0 };
|
---|
| 123 | const char overlong3[] = { 0b11110000, 0b10000000, 0 };
|
---|
| 124 |
|
---|
| 125 | const char overlong4[] = { 0b11000001, 0b10111111, 0 };
|
---|
| 126 | const char overlong5[] = { 0b11100000, 0b10011111, 0b10111111, 0 };
|
---|
| 127 | const char overlong6[] = { 0b11110000, 0b10001111, 0b10111111, 0b10111111, 0 };
|
---|
| 128 |
|
---|
| 129 | size_t offset = 0;
|
---|
| 130 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, str_decode(overlong1, &offset, sizeof(overlong1)));
|
---|
| 131 | offset = 0;
|
---|
| 132 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, str_decode(overlong2, &offset, sizeof(overlong2)));
|
---|
| 133 | offset = 0;
|
---|
| 134 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, str_decode(overlong3, &offset, sizeof(overlong3)));
|
---|
| 135 | offset = 0;
|
---|
| 136 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, str_decode(overlong4, &offset, sizeof(overlong4)));
|
---|
| 137 | offset = 0;
|
---|
| 138 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, str_decode(overlong5, &offset, sizeof(overlong5)));
|
---|
| 139 | offset = 0;
|
---|
| 140 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, str_decode(overlong6, &offset, sizeof(overlong6)));
|
---|
| 141 |
|
---|
| 142 | char sanitized[sizeof(overlong6)];
|
---|
| 143 | str_cpy(sanitized, STR_NO_LIMIT, overlong1);
|
---|
| 144 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, sanitized[0]);
|
---|
| 145 | str_cpy(sanitized, STR_NO_LIMIT, overlong2);
|
---|
| 146 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, sanitized[0]);
|
---|
| 147 | str_cpy(sanitized, STR_NO_LIMIT, overlong3);
|
---|
| 148 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, sanitized[0]);
|
---|
| 149 | str_cpy(sanitized, STR_NO_LIMIT, overlong4);
|
---|
| 150 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, sanitized[0]);
|
---|
| 151 | str_cpy(sanitized, STR_NO_LIMIT, overlong5);
|
---|
| 152 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, sanitized[0]);
|
---|
| 153 | str_cpy(sanitized, STR_NO_LIMIT, overlong6);
|
---|
| 154 | PCUT_ASSERT_INT_EQUALS(U_SPECIAL, sanitized[0]);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[a18a8b9] | 157 | PCUT_EXPORT(str);
|
---|