From 056f71f24cefaf88f2a93714c6678c03ed5f1e0e Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 02 七月 2021 16:54:33 +0800
Subject: [PATCH] fixed to adapt gcc-5.4 & glibc-2.25
---
utest/simple_tests.cpp | 172 +++++++++++++++++++++++----------------------------------
1 files changed, 70 insertions(+), 102 deletions(-)
diff --git a/utest/simple_tests.cpp b/utest/simple_tests.cpp
index eff0209..c7f277c 100644
--- a/utest/simple_tests.cpp
+++ b/utest/simple_tests.cpp
@@ -18,126 +18,94 @@
#include "util.h"
-struct s1000 { char a[1000]; };
-
+struct s1000 {
+ char a[1000];
+};
BOOST_AUTO_TEST_CASE(BasicTest)
{
- const std::string shm_name("basic");
- ShmRemover auto_remove(shm_name);
- SharedMemory shm(shm_name, 1024*1024*10);
- auto Avail = [&]() { return shm.get_free_memory(); };
+ const std::string shm_name("basic");
+ ShmRemover auto_remove(shm_name);
+ SharedMemory shm(shm_name, 1024 * 1024 * 10);
+ auto Avail = [&]() { return shm.get_free_memory(); };
- offset_ptr<const void> p;
- BOOST_CHECK(!p);
- BOOST_CHECK(p.get() == 0);
- p = 0;
- BOOST_CHECK(!p);
- BOOST_CHECK(p.get() == 0);
- const char *str = "basic";
- p = str;
- BOOST_CHECK(p);
- BOOST_CHECK(p.get() == str);
- p = 0;
- BOOST_CHECK(!p);
- BOOST_CHECK(p.get() == 0);
+ auto init_avail = Avail();
+ auto BasicTest = [&](int tid, int nloop) {
+ auto Code = [&](int id) {
+ typedef NamedShmObject<s1000> Int;
+ std::string name = std::to_string(id);
+ auto a0 = Avail();
+ Int i1(shm, name, eOpenOrCreate);
+ auto a1 = Avail();
+ BOOST_CHECK_LT(a1, a0);
+ printf("s1000 size: %ld\n", a0 - a1);
+ i1->a[0] = 5;
+ Int i2(shm, name, eOpenOrCreate);
+ auto a2 = Avail();
+ BOOST_CHECK_EQUAL(a1, a2);
+ BOOST_CHECK_EQUAL(i1.data(), i2.data());
+ int i = i1.Remove();
+ BOOST_CHECK_EQUAL(Avail(), a0);
- auto init_avail = Avail();
+ {
+ auto old = Avail();
+ void *p = shm.Alloc(1024);
+ shm.Dealloc(p);
+ BOOST_CHECK_EQUAL(old, Avail());
+ }
- auto BasicTest = [&](int tid, int nloop) {
- auto Code = [&](int id) {
+ bool r = shared_memory_object::remove(shm_name.c_str());
+ BOOST_CHECK(r);
+ };
+ for (int i = 0; i < nloop; ++i) {
+ Code(i + tid * nloop);
+ }
+ };
- typedef ShmObject<s1000> Int;
- std::string name = std::to_string(id);
- auto a0 = Avail();
- Int i1(shm, name);
- auto a1 = Avail();
- BOOST_CHECK_LT(a1, a0);
- printf("s1000 size: %ld\n", a0 - a1);
- i1->a[0] = 5;
- Int i2(shm, name);
- auto a2 = Avail();
- BOOST_CHECK_EQUAL(a1, a2);
- BOOST_CHECK_EQUAL(i1.data(), i2.data());
- int i = i1.Remove();
- BOOST_CHECK_EQUAL(Avail(), a0);
-
- {
- auto old = Avail();
- void *p = shm.Alloc(1024);
- shm.Dealloc(p);
- BOOST_CHECK_EQUAL(old, Avail());
- }
-
- bool r = shared_memory_object::remove(shm_name.c_str());
- BOOST_CHECK(r);
- };
- for (int i = 0; i < nloop; ++i) {
- Code(i + tid*nloop);
- }
- };
-
- // boost::timer::auto_cpu_timer timer;
- ThreadManager threads;
- int nthread = 1;
- int nloop = 1;
- for (int i = 0; i < nthread; ++i)
- {
- threads.Launch(BasicTest, i, nloop);
- }
- BOOST_CHECK_EQUAL(init_avail, Avail());
+ // boost::timer::auto_cpu_timer timer;
+ ThreadManager threads;
+ int nthread = 1;
+ int nloop = 1;
+ for (int i = 0; i < nthread; ++i) {
+ threads.Launch(BasicTest, i, nloop);
+ }
+ BOOST_CHECK_EQUAL(init_avail, Avail());
}
BOOST_AUTO_TEST_CASE(ForkTest)
{
- ProcessManager procs;
- const int nproc = 10;
+ ProcessManager procs;
+ const int nproc = 10;
- printf("Testing fork:\n");
+ printf("Testing fork:\n");
- auto child = [&](int id) {
- std::this_thread::sleep_for(100ms *id);
- printf("child id: %3d/%d ends\r", id, nproc);
- };
+ auto child = [&](int id) {
+ std::this_thread::sleep_for(100ms * id);
+ printf("child id: %3d/%d ends\r", id, nproc);
+ };
- for (int i = 0; i < nproc; ++i) {
- procs.Launch(child, i+1);
- }
-}
-
-BOOST_AUTO_TEST_CASE(TimedWaitTest)
-{
- const std::string shm_name("shm_wait");
- ShmRemover auto_remove(shm_name);
- SharedMemory shm(shm_name, 1024*1024);
- ShmMsgQueue q(shm, 64);
- for (int i = 0; i < 2; ++i) {
- int ms = i * 100;
- printf("Timeout Test %4d: ", ms);
- boost::timer::auto_cpu_timer timer;
- BHMsg msg;
- bool r = q.Recv(msg, ms);
- BOOST_CHECK(!r);
- }
+ for (int i = 0; i < nproc; ++i) {
+ procs.Launch(child, i + 1);
+ }
}
BOOST_AUTO_TEST_CASE(RefCountTest)
{
- const std::string shm_name("ShmRefCount");
- ShmRemover auto_remove(shm_name);
- SharedMemory shm(shm_name, 1024*1024);
+ SharedMemory &shm = TestShm();
+ typedef MsgI Msg;
+ GlobalInit(shm);
- MsgI m0(shm.Alloc(1000), shm.New<RefCount>());
- BOOST_CHECK(m0.IsCounted());
- BOOST_CHECK_EQUAL(m0.Count(), 1);
- MsgI m1 = m0;
- BOOST_CHECK(m1.IsCounted());
- BOOST_CHECK_EQUAL(m1.AddRef(), 2);
- BOOST_CHECK_EQUAL(m0.AddRef(), 3);
- BOOST_CHECK_EQUAL(m0.Release(shm), 2);
- BOOST_CHECK_EQUAL(m0.Release(shm), 1);
- BOOST_CHECK_EQUAL(m1.Release(shm), 0);
- BOOST_CHECK(!m1.IsCounted());
+ Msg m0(1000, shm);
+ BOOST_CHECK(!m0.valid());
+ m0.Make(100);
+ BOOST_CHECK_EQUAL(m0.Count(), 1);
+ Msg m1 = m0;
+ BOOST_CHECK(m1.valid());
+ BOOST_CHECK_EQUAL(m1.AddRef(), 2);
+ BOOST_CHECK_EQUAL(m0.AddRef(), 3);
+ BOOST_CHECK_EQUAL(m0.Release(), 2);
+ BOOST_CHECK_EQUAL(m0.Release(), 1);
+ BOOST_CHECK_EQUAL(m1.Release(), 0);
+ BOOST_CHECK(!m1.valid());
}
-
--
Gitblit v1.8.0