简体   繁体   English

谷歌测试:“从宽字符串初始化的字符数组”

[英]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.我已经实现了类型参数化测试( 示例 #6 )以将相同的测试用例应用于多个 class。 It happens that when assigning a string to either a signed char[] , unsigned char[] , const signed char[] or const unsigned char[] , I get:碰巧将字符串分配给signed char[]unsigned char[]const signed char[]const unsigned char[]时,我得到:

../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;在行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);创建了一个名为Implementations的类型列表,在下面的行中, TYPED_TEST_CASE(IosTest, Implementations); , is defined that the test case IosTest will be applied to the typed defined in the Implementations list. , 定义了测试用例IosTest将被应用到Implementations列表中定义的类型。

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).正如我已经说过的,如果我从Implementations列表中删除std::istreamstd::ostream ,我可以编译并运行测试而不会发出任何警告(我正在使用-Wall标志)。 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?是否有可能您的 gtest 库是使用您正在编译应用程序 (stackoverflow.cpp) 的不同版本编译器构建的? 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.我记得看到此错误消息与我使用较新版本的 gcc 构建的库相关,并尝试将其与旧版本的 gcc 链接。

You can try building gtest from source.您可以尝试从源代码构建 gtest。 It comes with a script that extracts and fuses everything into a single header file and a single cpp file.它带有一个脚本,可以将所有内容提取并融合到一个 header 文件和一个 cpp 文件中。

Look in your gtest installation for this python script:在您的 gtest 安装中查找此 python 脚本:

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-all.cc
  • gtest.h gtest.h

You only need to do this once and add it to your makefile.您只需执行一次并将其添加到您的 makefile 中。 I do exactly this for distributing a Linux-based app to a customer.我这样做是为了向客户分发基于 Linux 的应用程序。

It looks like GCC bug described here .它看起来像这里描述的 GCC 错误。

If you change signed char c[] = ".";如果你改变有signed char c[] = "."; to char c[] = ".";char c[] = "."; everything seems to work just fine.一切似乎都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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