sample2_unittest.cc
Go to the documentation of this file.
00001 // Copyright 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // A sample program demonstrating using Google C++ testing framework.
00031 //
00032 // Author: wan@google.com (Zhanyong Wan)
00033 
00034 
00035 // This sample shows how to write a more complex unit test for a class
00036 // that has multiple member functions.
00037 //
00038 // Usually, it's a good idea to have one test for each method in your
00039 // class.  You don't have to do that exactly, but it helps to keep
00040 // your tests organized.  You may also throw in additional tests as
00041 // needed.
00042 
00043 #include "sample2.h"
00044 #include "gtest/gtest.h"
00045 
00046 // In this example, we test the MyString class (a simple string).
00047 
00048 // Tests the default c'tor.
00049 TEST(MyString, DefaultConstructor) {
00050   const MyString s;
00051 
00052   // Asserts that s.c_string() returns NULL.
00053   //
00054   // <TechnicalDetails>
00055   //
00056   // If we write NULL instead of
00057   //
00058   //   static_cast<const char *>(NULL)
00059   //
00060   // in this assertion, it will generate a warning on gcc 3.4.  The
00061   // reason is that EXPECT_EQ needs to know the types of its
00062   // arguments in order to print them when it fails.  Since NULL is
00063   // #defined as 0, the compiler will use the formatter function for
00064   // int to print it.  However, gcc thinks that NULL should be used as
00065   // a pointer, not an int, and therefore complains.
00066   //
00067   // The root of the problem is C++'s lack of distinction between the
00068   // integer number 0 and the null pointer constant.  Unfortunately,
00069   // we have to live with this fact.
00070   //
00071   // </TechnicalDetails>
00072   EXPECT_STREQ(NULL, s.c_string());
00073 
00074   EXPECT_EQ(0u, s.Length());
00075 }
00076 
00077 const char kHelloString[] = "Hello, world!";
00078 
00079 // Tests the c'tor that accepts a C string.
00080 TEST(MyString, ConstructorFromCString) {
00081   const MyString s(kHelloString);
00082   EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
00083   EXPECT_EQ(sizeof(kHelloString)/sizeof(kHelloString[0]) - 1,
00084             s.Length());
00085 }
00086 
00087 // Tests the copy c'tor.
00088 TEST(MyString, CopyConstructor) {
00089   const MyString s1(kHelloString);
00090   const MyString s2 = s1;
00091   EXPECT_EQ(0, strcmp(s2.c_string(), kHelloString));
00092 }
00093 
00094 // Tests the Set method.
00095 TEST(MyString, Set) {
00096   MyString s;
00097 
00098   s.Set(kHelloString);
00099   EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
00100 
00101   // Set should work when the input pointer is the same as the one
00102   // already in the MyString object.
00103   s.Set(s.c_string());
00104   EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
00105 
00106   // Can we set the MyString to NULL?
00107   s.Set(NULL);
00108   EXPECT_STREQ(NULL, s.c_string());
00109 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06