Google 测试:“从宽字符串初始化的字符数组”
我已经实现了类型参数化测试(示例 #6) 将相同的测试用例应用于多个类。当将字符串分配给 signed char[]
、unsigned char[]
、constsigned 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
更有趣的是,当将测试用例应用于一种类型时,一切都很好,但是当我添加第二种类型时,它就会崩溃。我可以在以下代码中重现该错误:
#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]);
};
在 typedef Types
创建了一个名为 Implementations
的类型列表,并在下一行 TYPED_TEST_CASE(IosTest, Implements);
中定义了测试用例 IosTest
将应用于 Implementations
列表中定义的类型。
正如我已经说过的,如果我从 Implementations
列表中删除 std::istream
或 std::ostream
,我可以编译并运行测试时没有任何警告(我正在使用 -Wall
标志)。谁能解释这个现象吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 gtest 库是否可能是使用您正在编译应用程序(stackoverflow.cpp)的不同版本编译器构建的?我记得看到此错误消息与我使用较新版本的 gcc 构建的库相关,并尝试将其与旧版本的 gcc 链接。
您可以尝试从源代码构建 gtest。它附带一个脚本,可将所有内容提取并融合到一个头文件和一个 cpp 文件中。
在您的 gtest 安装中查找此 python 脚本:
脚本中有有关如何运行它的说明。您最终会得到两个文件:
您只需执行一次并将其添加到 makefile 中。我正是这样做的,以便向客户分发基于 Linux 的应用程序。
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:
There are instructions in the script for how to run it. You end up with two files:
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.
它看起来像此处描述的 GCC bug。
如果将
signed char c[] = ".";
更改为char c[] = ".";
一切似乎都工作得很好。It looks like GCC bug described here.
If you change
signed char c[] = ".";
tochar c[] = ".";
everything seems to work just fine.