1f92157deSopenharmony_ci// Copyright 2005, Google Inc. 2f92157deSopenharmony_ci// All rights reserved. 3f92157deSopenharmony_ci// 4f92157deSopenharmony_ci// Redistribution and use in source and binary forms, with or without 5f92157deSopenharmony_ci// modification, are permitted provided that the following conditions are 6f92157deSopenharmony_ci// met: 7f92157deSopenharmony_ci// 8f92157deSopenharmony_ci// * Redistributions of source code must retain the above copyright 9f92157deSopenharmony_ci// notice, this list of conditions and the following disclaimer. 10f92157deSopenharmony_ci// * Redistributions in binary form must reproduce the above 11f92157deSopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 12f92157deSopenharmony_ci// in the documentation and/or other materials provided with the 13f92157deSopenharmony_ci// distribution. 14f92157deSopenharmony_ci// * Neither the name of Google Inc. nor the names of its 15f92157deSopenharmony_ci// contributors may be used to endorse or promote products derived from 16f92157deSopenharmony_ci// this software without specific prior written permission. 17f92157deSopenharmony_ci// 18f92157deSopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19f92157deSopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20f92157deSopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21f92157deSopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22f92157deSopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23f92157deSopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24f92157deSopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25f92157deSopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26f92157deSopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27f92157deSopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28f92157deSopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29f92157deSopenharmony_ci 30f92157deSopenharmony_ci// A sample program demonstrating using Google C++ testing framework. 31f92157deSopenharmony_ci 32f92157deSopenharmony_ci// This sample shows how to write a more complex unit test for a class 33f92157deSopenharmony_ci// that has multiple member functions. 34f92157deSopenharmony_ci// 35f92157deSopenharmony_ci// Usually, it's a good idea to have one test for each method in your 36f92157deSopenharmony_ci// class. You don't have to do that exactly, but it helps to keep 37f92157deSopenharmony_ci// your tests organized. You may also throw in additional tests as 38f92157deSopenharmony_ci// needed. 39f92157deSopenharmony_ci 40f92157deSopenharmony_ci#include "sample2.h" 41f92157deSopenharmony_ci 42f92157deSopenharmony_ci#include "gtest/gtest.h" 43f92157deSopenharmony_cinamespace { 44f92157deSopenharmony_ci// In this example, we test the MyString class (a simple string). 45f92157deSopenharmony_ci 46f92157deSopenharmony_ci// Tests the default c'tor. 47f92157deSopenharmony_ciTEST(MyString, DefaultConstructor) { 48f92157deSopenharmony_ci const MyString s; 49f92157deSopenharmony_ci 50f92157deSopenharmony_ci // Asserts that s.c_string() returns NULL. 51f92157deSopenharmony_ci // 52f92157deSopenharmony_ci // <TechnicalDetails> 53f92157deSopenharmony_ci // 54f92157deSopenharmony_ci // If we write NULL instead of 55f92157deSopenharmony_ci // 56f92157deSopenharmony_ci // static_cast<const char *>(NULL) 57f92157deSopenharmony_ci // 58f92157deSopenharmony_ci // in this assertion, it will generate a warning on gcc 3.4. The 59f92157deSopenharmony_ci // reason is that EXPECT_EQ needs to know the types of its 60f92157deSopenharmony_ci // arguments in order to print them when it fails. Since NULL is 61f92157deSopenharmony_ci // #defined as 0, the compiler will use the formatter function for 62f92157deSopenharmony_ci // int to print it. However, gcc thinks that NULL should be used as 63f92157deSopenharmony_ci // a pointer, not an int, and therefore complains. 64f92157deSopenharmony_ci // 65f92157deSopenharmony_ci // The root of the problem is C++'s lack of distinction between the 66f92157deSopenharmony_ci // integer number 0 and the null pointer constant. Unfortunately, 67f92157deSopenharmony_ci // we have to live with this fact. 68f92157deSopenharmony_ci // 69f92157deSopenharmony_ci // </TechnicalDetails> 70f92157deSopenharmony_ci EXPECT_STREQ(nullptr, s.c_string()); 71f92157deSopenharmony_ci 72f92157deSopenharmony_ci EXPECT_EQ(0u, s.Length()); 73f92157deSopenharmony_ci} 74f92157deSopenharmony_ci 75f92157deSopenharmony_ciconst char kHelloString[] = "Hello, world!"; 76f92157deSopenharmony_ci 77f92157deSopenharmony_ci// Tests the c'tor that accepts a C string. 78f92157deSopenharmony_ciTEST(MyString, ConstructorFromCString) { 79f92157deSopenharmony_ci const MyString s(kHelloString); 80f92157deSopenharmony_ci EXPECT_EQ(0, strcmp(s.c_string(), kHelloString)); 81f92157deSopenharmony_ci EXPECT_EQ(sizeof(kHelloString) / sizeof(kHelloString[0]) - 1, s.Length()); 82f92157deSopenharmony_ci} 83f92157deSopenharmony_ci 84f92157deSopenharmony_ci// Tests the copy c'tor. 85f92157deSopenharmony_ciTEST(MyString, CopyConstructor) { 86f92157deSopenharmony_ci const MyString s1(kHelloString); 87f92157deSopenharmony_ci const MyString s2 = s1; 88f92157deSopenharmony_ci EXPECT_EQ(0, strcmp(s2.c_string(), kHelloString)); 89f92157deSopenharmony_ci} 90f92157deSopenharmony_ci 91f92157deSopenharmony_ci// Tests the Set method. 92f92157deSopenharmony_ciTEST(MyString, Set) { 93f92157deSopenharmony_ci MyString s; 94f92157deSopenharmony_ci 95f92157deSopenharmony_ci s.Set(kHelloString); 96f92157deSopenharmony_ci EXPECT_EQ(0, strcmp(s.c_string(), kHelloString)); 97f92157deSopenharmony_ci 98f92157deSopenharmony_ci // Set should work when the input pointer is the same as the one 99f92157deSopenharmony_ci // already in the MyString object. 100f92157deSopenharmony_ci s.Set(s.c_string()); 101f92157deSopenharmony_ci EXPECT_EQ(0, strcmp(s.c_string(), kHelloString)); 102f92157deSopenharmony_ci 103f92157deSopenharmony_ci // Can we set the MyString to NULL? 104f92157deSopenharmony_ci s.Set(nullptr); 105f92157deSopenharmony_ci EXPECT_STREQ(nullptr, s.c_string()); 106f92157deSopenharmony_ci} 107f92157deSopenharmony_ci} // namespace 108