我找到了一页链接:Convert Three.js to .stl for 3D Printing?
var exporter = new THREE.STLExporter();
var str = exporter.parse(scene);
console.log(str);
Run Code Online (Sandbox Code Playgroud)
但是当我使用它们时,不导出 stl 文件。
那我该怎么办呢?
当尝试使用 google test 模拟函数时,我仍然收到一条警告,指出我错过了“覆盖”
MOCK_METHOD(bool, functionName, (), (const override));
Run Code Online (Sandbox Code Playgroud)
我收到的警告指出
xxx.h:31:22: error: 'functionName' overrides a member function but is not marked 'override'
xxx.h:25:17: note: overridden virtual function is here
Run Code Online (Sandbox Code Playgroud)
如何同时使用override和标记模拟函数?const
std::shared_ptr<Dog> pd;
void F() {
pd = std::make_shared<Dog>("Smokey");
}
int main() {
std::thread t1(F);
std::thread t2(F);
t1.join();
t2.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
std::shared_ptr<Dog> pd(new Dog("Gunner"));
void F() {
std::shared_ptr<Dog> localCopy = pd;
}
int main() {
std::thread t1(F);
std::thread t2(F);
t1.join();
t2.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在 C++ 中,我知道 std::shared_ptr 对于读取和复制来说是线程安全的。但我对何时需要使用互斥体来同步线程有点困惑。我有两个代码片段。在第一个中,std::shared_ptr 被多个线程修改。在第二个线程中,每个线程仅读取和复制共享指针。在这两种情况下我都需要互斥体,还是只在第一种情况下需要互斥体?为什么或者为什么不?”
给定接口
class IFooable {
virtual void Fooable() = 0;
};
class IFoo {
virtual void Foo(IFooable* pFooable) = 0;
};
Run Code Online (Sandbox Code Playgroud)
和古尔模拟模拟
class TMockFoo : public IFoo {
MOCK_METHOD1(Foo, void (IFooable*));
};
Run Code Online (Sandbox Code Playgroud)
指定调用Fooable()模拟方法参数的操作的最简单方法是什么Foo()?
我努力了
TMockFoo MockFoo;
ON_CALL(MockFoo, Foo(_))
.WithArg<0>(Invoke(&IFooable::Fooable));
Run Code Online (Sandbox Code Playgroud)
但这不会编译,因为Invoke()只有一个参数需要一个自由函数,而不是成员函数。
使用boost::bind应该可以工作,但不一定会使代码太可读。在编写自定义之前Action,我想检查一下是否遗漏了一些完全明显的东西。
假设我有这个类,并且类型 Manager 在 Base.h 中向前声明。
#include <Base.h>
class MockBase : public Base
{
public:
MOCK_CONST_METHOD0( manager, const Manager&( ) );
...
};
Run Code Online (Sandbox Code Playgroud)
我不会在测试中使用此方法,因此我不想将 Manager 类的定义包含到 test.txt 文件中。
但我认为在编译 gmock 时尝试准备错误消息并深入其内部,它需要 Manager 变量的地址,并且我有一个错误:
错误 C2027:使用未定义类型“Manager”\external\googlemock\gtest\include\gtest\gtest-printers.h 146 1
我可以以某种方式避免包含带有我不会使用的方法的前向声明类型定义的文件吗?
我在某个类中注入依赖项。此类存储与 an 的依赖关系std::unique_ptr,因此是该对象的唯一所有者。
在这种依赖关系中模拟方法的正确方法是什么?unique_ptr我当前的解决方案是在移交所有权之前获取原始指针。虽然这有效,但我认为还有更好的方法可以做到这一点。这些是什么?
class Dependency
{
public:
virtual int plusOne(int x) {return x+1;}
};
class Dependency_Mock : public Dependency
{
public:
MOCK_METHOD1(plusOne, int(int));
};
class SomeClass
{
public:
void inject(std::unique_ptr<Dependency> dep) {dependency = std::move(dep);}
int execute(int x) {return dependency->plusOne(x);}
private:
std::unique_ptr<Dependency> dependency;
};
TEST(SomeClassTest, executeTestWithMock)
{
SomeClass some;
auto dep = std::make_unique<Dependency_Mock>();
auto& dep_ref = *(dep.get()); // This is kind of ugly.
some.inject(std::move(dep));
EXPECT_CALL( dep_ref , plusOne(_))
.WillOnce(Return(5));
EXPECT_EQ(some.execute(5), 5); // execute
}
Run Code Online (Sandbox Code Playgroud) 如何使用 gmock 匹配 C++ 元组中的一个元素?
例如,让我们尝试std::string从 中提取std::tuple<std::string, int>。
我知道我可以编写一个像这样的自定义匹配器:
MATCHER_P(match0thOfTuple, expected, "") { return (std::get<0>(arg) == expected); }
Run Code Online (Sandbox Code Playgroud)
但既然我找到了Pair(m1, m2)的匹配器std::pair,我期望也能找到类似的东西std::tuple。
Gmock 用于Args<N1, N2, ..., Nk>(m)选择元组参数的子集。当仅使用 1 个参数时,它仍然需要一个元组匹配器。以下尝试似乎无法编译:
struct {
MOCK_METHOD1(mockedFunction, void(std::tuple<std::string, int>&));
} mock;
EXPECT_CALL(mock, mockedFunction(testing::Args<0>(testing::Eq(expectedStringValue))));
Run Code Online (Sandbox Code Playgroud)
并使我的 clang 给出如下编译错误:
.../gtest/googlemock/include/gmock/gmock-matchers.h:204:60: error: invalid operands to binary expression ('const std::__1::tuple<std::__1::basic_string<char> >' and 'const std::__1::basic_string<char>')
bool operator()(const A& a, const B& b) const { return a == b; }
...
Run Code Online (Sandbox Code Playgroud)
是否有 …
我想将参数化测试与类型化测试混合在一起。这是我的尝试:
struct X {};
struct Y {};
template <typename T>
struct MyTestFixture: public ::testing::Test
{
T t;
};
template <typename T, typename Param>
struct MyTestFixtureWithParam : public MyTestFixture<T>,
public ::testing::WithParamInterface<Param>
{
};
using MyTestFixtureWithXandString = MyTestFixtureWithParam<X, std::string>;
TEST_P(MyTestFixtureWithXandString, Test1)
{
}
INSTANTIATE_TEST_CASE_P(Name, MyTestFixtureWithXandString,
::testing::Values("a", "b"));
using MyTestFixtureWithYandString = MyTestFixtureWithParam<Y, std::string>;
TEST_P(MyTestFixtureWithYandString, Test1)
{
}
INSTANTIATE_TEST_CASE_P(Name, MyTestFixtureWithYandString,
::testing::Values("a", "b"));
Run Code Online (Sandbox Code Playgroud)
Gtest 是否包含一些混合 TYPED_TEST 和 TEST_P 的宏,或者上面的代码是实现我的目标的唯一方法?
对的调用ImGui::InputText()采用一个char数组,我需要从 a 初始化该数组std::string,然后将内容传输回std::string. 最简单的形式:
char buf[255]{};
std::string s{"foo"};
void fn() {
strncpy( buf, s.c_str(), sizeof(buf)-1 );
ImGui::InputText( "Text", buf, sizeof(buf) );
s=buf;
}
Run Code Online (Sandbox Code Playgroud)
然而,让两个缓冲区(buf以及其中分配的缓冲区std::string)都做同样的事情似乎很浪费。我可以通过仅使用一个简单的包装器“X”来避免buf缓冲区以及从缓冲区进行复制吗?std::string我不关心效率,我只想要调用站点最简单的代码。这段代码确实有效,但它安全吗?有更好的方法吗?
class X {
public:
X(std::string& s) : s_{s} { s.resize(len_); }
~X() { s_.resize(strlen(s_.c_str())); }
operator char*(){ return s_.data(); }
static constexpr auto len() { return len_-1; }
private:
std::string& s_;
static constexpr auto len_=255;
};
std::string s{"foo"};
void fn() …Run Code Online (Sandbox Code Playgroud) 背景:
\n我正在模拟硬件 i2c 传输函数,并尝试匹配传递给它的数组。这是在嵌入式环境中,因此需要 C 风格的数组并且缺少 STL 容器。
\n尝试匹配我的第二个参数,即 c 样式数组(下面的缓冲区),在编译时失败。
\n设置:
\n接口定义为:
\nvirtual I2C_Status_e i2c1Transmit(uint8_t address, \n const uint8_t buffer[], \n uint8_t length) = 0;\nRun Code Online (Sandbox Code Playgroud)\n我将我的测试模拟设置为:
\nMOCK_METHOD(I2C_Status_e,\n i2c1Transmit,\n (uint8_t address, const uint8_t buffer[], uint8_t length),\n (override));\nRun Code Online (Sandbox Code Playgroud)\n二进制文件中的调用点(此传输发送单个 8 位命令):
\nstatus = hal->i2c1Transmit(address, readStatusCommand, 1);\nRun Code Online (Sandbox Code Playgroud)\n其中 readStatusCommand 默认为:
\nconst uint8_t readStatusCommand[1] = {0x00};\nRun Code Online (Sandbox Code Playgroud)\n测试代码:
\n这里 readStatusCommand 具有与上面相同的签名。
\nEXPECT_CALL(hal, i2c1Transmit(address0, ElementsAreArray(readStatusCommand, 1), txLength))\n .WillOnce(Return(HAL_Ok));\nRun Code Online (Sandbox Code Playgroud)\n编译器输出:
\n …c++ ×9
googlemock ×7
googletest ×5
3d ×1
c++17 ×1
imgui ×1
javascript ×1
shared-ptr ×1
three.js ×1