mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
add ut
This commit is contained in:
parent
e5f722937e
commit
2fb49eb3c4
@ -7,6 +7,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "libipc/def.h"
|
#include "libipc/def.h"
|
||||||
|
#include "libipc/export.h"
|
||||||
#include "libipc/result.h"
|
#include "libipc/result.h"
|
||||||
|
|
||||||
LIBIPC_NAMESPACE_BEG_
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
|||||||
73
test/test_result.cpp
Normal file
73
test/test_result.cpp
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
#include "libipc/result.h"
|
||||||
|
|
||||||
|
TEST(result, ok) {
|
||||||
|
ipc::result ret;
|
||||||
|
EXPECT_FALSE(ret);
|
||||||
|
EXPECT_FALSE(ret.ok());
|
||||||
|
EXPECT_EQ(ret.code(), 0);
|
||||||
|
|
||||||
|
ret = {true};
|
||||||
|
EXPECT_TRUE(ret);
|
||||||
|
EXPECT_TRUE(ret.ok());
|
||||||
|
EXPECT_EQ(ret.code(), 0);
|
||||||
|
|
||||||
|
ret = {false, 1234};
|
||||||
|
EXPECT_FALSE(ret);
|
||||||
|
EXPECT_FALSE(ret.ok());
|
||||||
|
EXPECT_EQ(ret.code(), 1234);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(result, code) {
|
||||||
|
ipc::result ret(true, 1234);
|
||||||
|
EXPECT_TRUE(ret);
|
||||||
|
EXPECT_TRUE(ret.ok());
|
||||||
|
EXPECT_EQ(ret.code(), 1234);
|
||||||
|
|
||||||
|
ret = {false};
|
||||||
|
EXPECT_FALSE(ret);
|
||||||
|
EXPECT_FALSE(ret.ok());
|
||||||
|
EXPECT_EQ(ret.code(), 0);
|
||||||
|
|
||||||
|
ret = {true, 4321};
|
||||||
|
EXPECT_TRUE(ret);
|
||||||
|
EXPECT_TRUE(ret.ok());
|
||||||
|
EXPECT_EQ(ret.code(), 4321);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(result, compare) {
|
||||||
|
ipc::result r1, r2;
|
||||||
|
EXPECT_EQ(r1, r2);
|
||||||
|
|
||||||
|
ipc::result r3(true);
|
||||||
|
EXPECT_NE(r1, r3);
|
||||||
|
|
||||||
|
ipc::result r4(true, 222222);
|
||||||
|
EXPECT_NE(r3, r4);
|
||||||
|
|
||||||
|
ipc::result r5(false, 222222);
|
||||||
|
EXPECT_NE(r4, r5);
|
||||||
|
r3 = r5;
|
||||||
|
EXPECT_EQ(r3, r5);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(result, print) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ipc::result r1;
|
||||||
|
ss << r1;
|
||||||
|
EXPECT_EQ(ss.str(), "[fail, code = 0]");
|
||||||
|
|
||||||
|
ss = {};
|
||||||
|
ipc::result r2(true, 65537);
|
||||||
|
ss << r2;
|
||||||
|
EXPECT_EQ(ss.str(), "[succ, code = 65537]");
|
||||||
|
|
||||||
|
ss = {};
|
||||||
|
ipc::result r3(true);
|
||||||
|
ss << r3;
|
||||||
|
EXPECT_EQ(ss.str(), "[succ, code = 0]");
|
||||||
|
}
|
||||||
@ -8,6 +8,9 @@
|
|||||||
#include "libipc/utility/construct.h"
|
#include "libipc/utility/construct.h"
|
||||||
#include "libipc/utility/pimpl.h"
|
#include "libipc/utility/pimpl.h"
|
||||||
#include "libipc/utility/countof.h"
|
#include "libipc/utility/countof.h"
|
||||||
|
#include "libipc/utility/horrible_cast.h"
|
||||||
|
|
||||||
|
#include "libipc/detect_plat.h"
|
||||||
|
|
||||||
TEST(utility, construct) {
|
TEST(utility, construct) {
|
||||||
struct Foo {
|
struct Foo {
|
||||||
@ -101,3 +104,25 @@ TEST(utility, countof) {
|
|||||||
EXPECT_EQ(ipc::countof(vec), vec.size());
|
EXPECT_EQ(ipc::countof(vec), vec.size());
|
||||||
EXPECT_EQ(ipc::countof(arr), sizeof(arr) / sizeof(arr[0]));
|
EXPECT_EQ(ipc::countof(arr), sizeof(arr) / sizeof(arr[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(utility, horrible_cast) {
|
||||||
|
struct A {
|
||||||
|
int a_;
|
||||||
|
} a {123};
|
||||||
|
|
||||||
|
struct B {
|
||||||
|
char a_[sizeof(int)];
|
||||||
|
} b = ipc::horrible_cast<B>(a);
|
||||||
|
|
||||||
|
EXPECT_EQ(b.a_[1], 0);
|
||||||
|
EXPECT_EQ(b.a_[2], 0);
|
||||||
|
#if LIBIPC_ENDIAN_LIT
|
||||||
|
EXPECT_EQ(b.a_[0], 123);
|
||||||
|
EXPECT_EQ(b.a_[3], 0);
|
||||||
|
#else
|
||||||
|
EXPECT_EQ(b.a_[3], 123);
|
||||||
|
EXPECT_EQ(b.a_[0], 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ipc::horrible_cast<std::uint32_t>(0ll);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user