简体   繁体   中英

Google Test: “char-array initialized from wide string”

I have implemented type-parameterized tests ( Sample #6 ) to apply the same test case to more than one class. It happens that when assigning a string to either a signed char[] , unsigned char[] , const signed char[] or const unsigned char[] , I get:

../stackoverflow.cpp: In member function ‘void IosTest_DummyTest_Test<gtest_TypeParam_>::TestBody() [with gtest_TypeParam_ = std::basic_istream<char, std::char_traits<char> >]’:                           
../stackoverflow.cpp:34:   instantiated from here
../stackoverflow.cpp:32: error: char-array initialized from wide string

What is more interesting is that when applying the test case to one type everything goes just fine, but when I add a second type it blows up. I could reproduce the error in the following code:

#include "gtest/gtest.h"
#include <iostream>

// Factory methods
template<class T> std::ios* CreateStream();

template<>
std::ios* CreateStream<std::istream>() {
  return &std::cin;
}

template<>
std::ios* CreateStream<std::ostream>() {
  return &std::cout;
}

// Fixture class
template<class T>
class IosTest: public ::testing::Test {
 protected:
  IosTest() : ios_(CreateStream<T>()) {}
  virtual ~IosTest() {}
  std::ios* const ios_;
};

using testing::Types;
typedef Types<std::istream, std::ostream> Implementations;
TYPED_TEST_CASE(IosTest, Implementations);

TYPED_TEST(IosTest, DummyTest) {
  signed char c[] = ".";
  this->ios_->fill(c[0]);
};

In the line typedef Types<std::istream, std::ostream> Implementations; is created a list of types called Implementations and in the following line, TYPED_TEST_CASE(IosTest, Implementations); , is defined that the test case IosTest will be applied to the typed defined in the Implementations list.

As I have already said, if I remove either std::istream or std::ostream from the Implementations list I can compile and run the tests without any warning (I am using the -Wall flag). Can anyone explain this phenomenon?

Is it is possible your gtest library was built with a different version compiler that you are compiling your app (stackoverflow.cpp) with? I recall seeing this error message related to a lib I had built with a newer version of gcc and trying to link it with an older version of gcc.

You can try building gtest from source. It comes with a script that extracts and fuses everything into a single header file and a single cpp file.

Look in your gtest installation for this python script:

gtest/scripts/fuse_gtest_files.py

There are instructions in the script for how to run it. You end up with two files:

  • gtest-all.cc
  • gtest.h

You only need to do this once and add it to your makefile. I do exactly this for distributing a Linux-based app to a customer.

It looks like GCC bug described here .

If you change signed char c[] = "."; to char c[] = "."; everything seems to work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM