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

发布于 2024-11-29 20:50:00 字数 1943 浏览 1 评论 0原文

我已经实现了类型参数化测试(示例 #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行中Implements; 创建了一个名为 Implementations 的类型列表,并在下一行 TYPED_TEST_CASE(IosTest, Implements); 中定义了测试用例 IosTest 将应用于 Implementations 列表中定义的类型。

正如我已经说过的,如果我从 Implementations 列表中删除 std::istreamstd::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨与橙与城 2024-12-06 20:50:00

您的 gtest 库是否可能是使用您正在编译应用程序(stackoverflow.cpp)的不同版本编译器构建的?我记得看到此错误消息与我使用较新版本的 gcc 构建的库相关,并尝试将其与旧版本的 gcc 链接。

您可以尝试从源代码构建 gtest。它附带一个脚本,可将所有内容提取并融合到一个头文件和一个 cpp 文件中。

在您的 gtest 安装中查找此 python 脚本:

gtest/scripts/fuse_gtest_files.py

脚本中有有关如何运行它的说明。您最终会得到两个文件:

  • gtest-all.cc
  • gtest.h

您只需执行一次并将其添加到 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:

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.

非要怀念 2024-12-06 20:50:00

它看起来像此处描述的 GCC bug。

如果将 signed char c[] = "."; 更改为 char c[] = "."; 一切似乎都工作得很好。

It looks like GCC bug described here.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文