1 | // |
2 | // SharedMemoryTest.cpp |
3 | // |
4 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "SharedMemoryTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/SharedMemory.h" |
15 | #include "Poco/Path.h" |
16 | #include "Poco/File.h" |
17 | #include "Poco/Exception.h" |
18 | #include "Poco/Environment.h" |
19 | #include <sstream> |
20 | #include <iostream> |
21 | |
22 | |
23 | using Poco::SharedMemory; |
24 | |
25 | |
26 | SharedMemoryTest::SharedMemoryTest(const std::string& rName): CppUnit::TestCase(rName) |
27 | { |
28 | } |
29 | |
30 | |
31 | SharedMemoryTest::~SharedMemoryTest() |
32 | { |
33 | } |
34 | |
35 | |
36 | void SharedMemoryTest::testCreate() |
37 | { |
38 | SharedMemory mem("hi" , 4096, SharedMemory::AM_WRITE); |
39 | assertTrue (mem.end()-mem.begin() == 4096); |
40 | mem.begin()[0] = 'A'; |
41 | mem.end()[-1] = 'Z'; |
42 | } |
43 | |
44 | |
45 | void SharedMemoryTest::testCreateFromFile() |
46 | { |
47 | Poco::Path p = findDataFile("data" , "testdata.txt" ); |
48 | Poco::File f(p); |
49 | assertTrue (f.exists() && f.isFile()); |
50 | SharedMemory mem(f, SharedMemory::AM_READ); |
51 | assertTrue (mem.end() > mem.begin()); // valid? |
52 | assertTrue (mem.end() - mem.begin() == f.getSize()); |
53 | assertTrue (mem.begin()[0] == 'A'); |
54 | assertTrue (mem.end()[-5] == 'Z'); |
55 | } |
56 | |
57 | |
58 | Poco::Path SharedMemoryTest::findDataFile(const std::string& directory, const std::string& file) |
59 | { |
60 | std::ostringstream ostr; |
61 | ostr << directory << '/' << file; |
62 | std::string validDir(ostr.str()); |
63 | Poco::Path pathPattern(validDir); |
64 | if (Poco::File(pathPattern).exists()) |
65 | { |
66 | return pathPattern; |
67 | } |
68 | |
69 | ostr.str("" ); |
70 | ostr << "/Foundation/testsuite/" << directory << '/' << file; |
71 | validDir = Poco::Environment::get("POCO_BASE" ) + ostr.str(); |
72 | pathPattern = validDir; |
73 | |
74 | if (!Poco::File(pathPattern).exists()) |
75 | { |
76 | std::cout << "Can't find " << validDir << std::endl; |
77 | throw Poco::NotFoundException("cannot locate directory containing valid Zip test files" ); |
78 | } |
79 | return pathPattern; |
80 | } |
81 | |
82 | |
83 | void SharedMemoryTest::setUp() |
84 | { |
85 | } |
86 | |
87 | |
88 | void SharedMemoryTest::tearDown() |
89 | { |
90 | } |
91 | |
92 | |
93 | CppUnit::Test* SharedMemoryTest::suite() |
94 | { |
95 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SharedMemoryTest" ); |
96 | |
97 | #if !defined(POCO_NO_SHAREDMEMORY) |
98 | CppUnit_addTest(pSuite, SharedMemoryTest, testCreate); |
99 | CppUnit_addTest(pSuite, SharedMemoryTest, testCreateFromFile); |
100 | #endif |
101 | return pSuite; |
102 | } |
103 | |