blob: ec3d714b36cb7855f3e8ea3efc1afb1310dddb75 [file] [log] [blame]
Mike Frysinger32c39922018-01-17 17:09:54 -05001// util_unittest.cpp
2// Copyright (C) 2018 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// Test system.[ch] module code using gtest.
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <sys/stat.h>
22#include <unistd.h>
23
24#include <gtest/gtest.h>
25
26#include "util.h"
27
28// Sanity check for the strip func.
29TEST(strip, basic) {
30 char str[] = " foo\t";
31 ASSERT_EQ("foo", std::string(strip(str)));
32}
33
34// Make sure we don't crash with various "null"-like inputs.
35TEST(tokenize, null_stringp) {
36 ASSERT_EQ(nullptr, tokenize(nullptr, nullptr));
37 ASSERT_EQ(nullptr, tokenize(nullptr, ""));
38 ASSERT_EQ(nullptr, tokenize(nullptr, ","));
39
40 char *p = nullptr;
41 ASSERT_EQ(nullptr, tokenize(&p, nullptr));
42}
43
44// Make sure we don't crash with various "null"-like inputs.
45TEST(tokenize, null_delim) {
46 char str[] = "a,b,c";
47 char *p = str;
48 ASSERT_EQ(str, tokenize(&p, nullptr));
49 ASSERT_EQ(nullptr, p);
50 ASSERT_EQ(str, std::string("a,b,c"));
51
52 p = str;
53 ASSERT_EQ(str, tokenize(&p, ""));
54 ASSERT_EQ(nullptr, p);
55 ASSERT_EQ(str, std::string("a,b,c"));
56}
57
58// Sanity check for the tokenize func.
59TEST(tokenize, basic) {
60 char str[] = "a,b,c";
61 char *p = str;
62 ASSERT_EQ("a", std::string(tokenize(&p, ",")));
63 ASSERT_EQ("b", std::string(tokenize(&p, ",")));
64 ASSERT_EQ("c", std::string(tokenize(&p, ",")));
65 ASSERT_EQ(nullptr, p);
66 ASSERT_EQ(nullptr, tokenize(&p, ","));
67}
Mike Frysingerb4c7e772018-01-17 17:40:15 -050068
69// Check edge case with an empty string.
70TEST(tokenize, empty_string) {
71 char str[] = "";
72 char *p = str;
73 ASSERT_EQ("", std::string(tokenize(&p, ",")));
74 ASSERT_EQ(nullptr, p);
75 ASSERT_EQ(nullptr, tokenize(&p, ","));
76}
77
78// Check behavior with empty tokens at the start/middle/end.
79TEST(tokenize, empty_tokens) {
80 char str[] = ",,a,b,,,c,,";
81 char *p = str;
82 ASSERT_EQ("", std::string(tokenize(&p, ",")));
83 ASSERT_EQ("", std::string(tokenize(&p, ",")));
84 ASSERT_EQ("a", std::string(tokenize(&p, ",")));
85 ASSERT_EQ("b", std::string(tokenize(&p, ",")));
86 ASSERT_EQ("", std::string(tokenize(&p, ",")));
87 ASSERT_EQ("", std::string(tokenize(&p, ",")));
88 ASSERT_EQ("c", std::string(tokenize(&p, ",")));
89 ASSERT_EQ("", std::string(tokenize(&p, ",")));
90 ASSERT_EQ("", std::string(tokenize(&p, ",")));
91 ASSERT_EQ(nullptr, p);
92 ASSERT_EQ(nullptr, tokenize(&p, ","));
93}