Mike Frysinger | 32c3992 | 2018-01-17 17:09:54 -0500 | [diff] [blame] | 1 | // 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. |
| 29 | TEST(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. |
| 35 | TEST(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. |
| 45 | TEST(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. |
| 59 | TEST(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 Frysinger | b4c7e77 | 2018-01-17 17:40:15 -0500 | [diff] [blame] | 68 | |
| 69 | // Check edge case with an empty string. |
| 70 | TEST(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. |
| 79 | TEST(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 | } |