1/* Copyright 2018 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 * 5 * Test util.[ch] module code using gtest. 6 */ 7 8#include <stdio.h> 9#include <stdlib.h> 10#include <string.h> 11#include <sys/stat.h> 12#include <unistd.h> 13 14#include <gtest/gtest.h> 15 16#include "util.h" 17 18// Sanity check for the strip func. 19TEST(strip, basic) { 20 char str[] = " foo\t"; 21 ASSERT_EQ("foo", std::string(strip(str))); 22} 23 24// Make sure we don't crash with various "null"-like inputs. 25TEST(tokenize, null_stringp) { 26 ASSERT_EQ(nullptr, tokenize(nullptr, nullptr)); 27 ASSERT_EQ(nullptr, tokenize(nullptr, "")); 28 ASSERT_EQ(nullptr, tokenize(nullptr, ",")); 29 30 char *p = nullptr; 31 ASSERT_EQ(nullptr, tokenize(&p, nullptr)); 32} 33 34// Make sure we don't crash with various "null"-like inputs. 35TEST(tokenize, null_delim) { 36 char str[] = "a,b,c"; 37 char *p = str; 38 ASSERT_EQ(str, tokenize(&p, nullptr)); 39 ASSERT_EQ(nullptr, p); 40 ASSERT_EQ(str, std::string("a,b,c")); 41 42 p = str; 43 ASSERT_EQ(str, tokenize(&p, "")); 44 ASSERT_EQ(nullptr, p); 45 ASSERT_EQ(str, std::string("a,b,c")); 46} 47 48// Sanity check for the tokenize func. 49TEST(tokenize, basic) { 50 char str[] = "a,b,c"; 51 char *p = str; 52 ASSERT_EQ("a", std::string(tokenize(&p, ","))); 53 ASSERT_EQ("b", std::string(tokenize(&p, ","))); 54 ASSERT_EQ("c", std::string(tokenize(&p, ","))); 55 ASSERT_EQ(nullptr, p); 56 ASSERT_EQ(nullptr, tokenize(&p, ",")); 57} 58 59// Check edge case with an empty string. 60TEST(tokenize, empty_string) { 61 char str[] = ""; 62 char *p = str; 63 ASSERT_EQ("", std::string(tokenize(&p, ","))); 64 ASSERT_EQ(nullptr, p); 65 ASSERT_EQ(nullptr, tokenize(&p, ",")); 66} 67 68// Check behavior with empty tokens at the start/middle/end. 69TEST(tokenize, empty_tokens) { 70 char str[] = ",,a,b,,,c,,"; 71 char *p = str; 72 ASSERT_EQ("", std::string(tokenize(&p, ","))); 73 ASSERT_EQ("", std::string(tokenize(&p, ","))); 74 ASSERT_EQ("a", std::string(tokenize(&p, ","))); 75 ASSERT_EQ("b", std::string(tokenize(&p, ","))); 76 ASSERT_EQ("", std::string(tokenize(&p, ","))); 77 ASSERT_EQ("", std::string(tokenize(&p, ","))); 78 ASSERT_EQ("c", std::string(tokenize(&p, ","))); 79 ASSERT_EQ("", std::string(tokenize(&p, ","))); 80 ASSERT_EQ("", std::string(tokenize(&p, ","))); 81 ASSERT_EQ(nullptr, p); 82 ASSERT_EQ(nullptr, tokenize(&p, ",")); 83} 84