File: string_tests.cpp

package info (click to toggle)
intel-compute-runtime 20.44.18297-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,780 kB
  • sloc: cpp: 379,729; lisp: 4,931; python: 299; sh: 196; makefile: 8
file content (150 lines) | stat: -rw-r--r-- 3,986 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (C) 2017-2020 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/helpers/hash.h"
#include "shared/source/helpers/string.h"

#include "gtest/gtest.h"

#if defined(__linux__)

TEST(StringHelpers, strncpy) {
    char dst[1024] = "";
    char src[1024] = "HelloWorld";

    //preconditions
    ASSERT_EQ(sizeof(dst), sizeof(src));
    //String must be smaller than array capacity
    ASSERT_LT(strlen(src), sizeof(src));

    auto ret = strncpy_s(nullptr, 1024, src, 1024);
    EXPECT_EQ(ret, -EINVAL);

    ret = strncpy_s(dst, 1024, nullptr, 1024);
    EXPECT_EQ(ret, -EINVAL);

    ret = strncpy_s(dst, 512, src, 1024);
    EXPECT_EQ(ret, -ERANGE);

    memset(dst, 0, sizeof(dst));
    ret = strncpy_s(dst, 1024, src, strlen(src) / 2);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(0, memcmp(dst, src, strlen(src) / 2));
    for (size_t i = strlen(src) / 2; i < sizeof(dst); i++)
        EXPECT_EQ(0, dst[i]);

    memset(dst, 0, sizeof(dst));
    ret = strncpy_s(dst, strlen(src) / 2, src, strlen(src) / 2);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(0, memcmp(dst, src, strlen(src) / 2));
    for (size_t i = strlen(src) / 2; i < sizeof(dst); i++)
        EXPECT_EQ(0, dst[i]);

    strncpy_s(dst, 1024, src, 1024);
    EXPECT_EQ(0, memcmp(dst, src, strlen(src)));
    for (size_t i = strlen(src); i < sizeof(dst); i++)
        EXPECT_EQ(0, dst[i]);
}

TEST(StringHelpers, memmove) {
    char dst[1024] = "";
    char src[1024] = "HelloWorld";

    ASSERT_EQ(sizeof(dst), sizeof(src));

    auto ret = memmove_s(nullptr, sizeof(dst), src, sizeof(src));
    EXPECT_EQ(ret, -EINVAL);

    ret = memmove_s(dst, sizeof(dst), nullptr, sizeof(src));
    EXPECT_EQ(ret, -EINVAL);

    ret = memmove_s(dst, sizeof(src) / 2, src, sizeof(src));
    EXPECT_EQ(ret, -ERANGE);

    memset(dst, 0, sizeof(dst));
    ret = memmove_s(dst, sizeof(dst), src, sizeof(src));
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(0, memcmp(dst, src, sizeof(dst)));
}

TEST(StringHelpers, strcpy) {
    char dst[1024] = "";
    char src[1024] = "HelloWorld";

    ASSERT_EQ(sizeof(dst), sizeof(src));

    auto ret = strcpy_s(nullptr, 0, src);
    EXPECT_EQ(ret, -EINVAL);

    ret = strcpy_s(nullptr, sizeof(dst), src);
    EXPECT_EQ(ret, -EINVAL);

    ret = strcpy_s(nullptr, 0, nullptr);
    EXPECT_EQ(ret, -EINVAL);

    ret = strcpy_s(nullptr, sizeof(dst), nullptr);
    EXPECT_EQ(ret, -EINVAL);

    ret = strcpy_s(dst, 0, nullptr);
    EXPECT_EQ(ret, -EINVAL);

    ret = strcpy_s(dst, strlen(src) / 2, src);
    EXPECT_EQ(ret, -ERANGE);

    ret = strcpy_s(dst, strlen(src), src);
    EXPECT_EQ(ret, -ERANGE);

    char pattern = 0x5a;
    memset(dst, pattern, sizeof(dst));
    ret = strcpy_s(dst, sizeof(dst), src);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(0, memcmp(dst, src, strlen(src)));
    EXPECT_EQ(0, dst[strlen(src)]);
    for (size_t i = strlen(src) + 1; i < sizeof(dst); i++)
        EXPECT_EQ(pattern, dst[i]);
}

TEST(StringHelpers, strnlen) {
    char src[1024] = "HelloWorld";

    auto ret = strnlen_s(nullptr, sizeof(src));
    EXPECT_EQ(ret, 0u);

    ret = strnlen_s(src, 0);
    EXPECT_EQ(ret, 0u);

    ret = strnlen_s(src, sizeof(src));
    EXPECT_EQ(ret, strlen(src));
}

TEST(StringHelpers, memcpy) {
    char dst[1024] = "";
    char src[1024] = "HelloWorld";

    //preconditions
    ASSERT_EQ(sizeof(dst), sizeof(src));
    //String must be smaller than array capacity
    ASSERT_LT(strlen(src), sizeof(src));

    auto ret = memcpy_s(nullptr, sizeof(dst), src, sizeof(src));
    EXPECT_EQ(ret, -EINVAL);

    ret = memcpy_s(dst, sizeof(dst), nullptr, sizeof(src));
    EXPECT_EQ(ret, -EINVAL);

    ret = memcpy_s(dst, sizeof(dst) / 2, src, sizeof(src));
    EXPECT_EQ(ret, -ERANGE);

    memset(dst, 0, sizeof(dst));
    ret = memcpy_s(dst, sizeof(dst), src, strlen(src) / 2);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(0, memcmp(dst, src, strlen(src) / 2));
    for (size_t i = strlen(src) / 2; i < sizeof(dst); i++)
        EXPECT_EQ(0, dst[i]);
}

#endif