1 /** @file
2 
3     Catch-based unit tests for MIOBufferWriter class.
4 
5     @section license License
6 
7     Licensed to the Apache Software Foundation (ASF) under one
8     or more contributor license agreements.  See the NOTICE file
9     distributed with this work for additional information
10     regarding copyright ownership.  The ASF licenses this file
11     to you under the Apache License, Version 2.0 (the
12     "License"); you may not use this file except in compliance
13     with the License.  You may obtain a copy of the License at
14 
15     http://www.apache.org/licenses/LICENSE-2.0
16 
17     Unless required by applicable law or agreed to in writing, software
18     distributed under the License is distributed on an "AS IS" BASIS,
19     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20     See the License for the specific language governing permissions and
21     limitations under the License.
22  */
23 
24 #define CATCH_CONFIG_RUNNER
25 #include "catch.hpp"
26 
27 #include <cstdint>
28 #include <cstdlib>
29 
30 #include "I_EventSystem.h"
31 #include "tscore/I_Layout.h"
32 
33 #include "diags.i"
34 #include "I_MIOBufferWriter.h"
35 
36 int
37 main(int argc, char *argv[])
38 {
39   // global setup...
40   Layout::create();
41   init_diags("", nullptr);
42   RecProcessInit(RECM_STAND_ALONE);
43 
44   ink_event_system_init(EVENT_SYSTEM_MODULE_PUBLIC_VERSION);
45   eventProcessor.start(2);
46 
47   Thread *main_thread = new EThread;
48   main_thread->set_specific();
49 
50   std::cout << "Pre-Catch" << std::endl;
51   int result = Catch::Session().run(argc, argv);
52 
53   // global clean-up...
54 
55   exit(result);
56 }
57 
58 std::string
59 genData(int numBytes)
60 {
61   static std::uint8_t genData;
62 
63   std::string s(numBytes, ' ');
64 
65   for (int i{0}; i < numBytes; ++i) {
66     s[i] = genData;
67     genData += 7;
68   }
69 
70   return s;
71 }
72 
73 void
74 writeOnce(MIOBufferWriter &bw, std::size_t len)
75 {
76   static bool toggle;
77 
78   std::string s{genData(len)};
79 
80   if (len == 1) {
81     bw.write(s[0]);
82 
83   } else if (toggle) {
84     std::size_t cap{bw.auxBufferCapacity()};
85 
86     if (cap >= len) {
87       memcpy(bw.auxBuffer(), s.data(), len);
88       bw.fill(len);
89 
90     } else {
91       memcpy(bw.auxBuffer(), s.data(), cap);
92       bw.fill(cap);
93       bw.write(s.data() + cap, len - cap);
94     }
95   } else {
96     bw.write(s.data(), len);
97   }
98 
99   toggle = !toggle;
100 
101   REQUIRE(bw.auxBufferCapacity() <= DEFAULT_BUFFER_NUMBER);
102 }
103 
104 class InkAssertExcept
105 {
106 };
107 
108 TEST_CASE("MIOBufferWriter", "[MIOBW]")
109 {
110   MIOBuffer *theMIOBuffer = new_MIOBuffer(default_large_iobuffer_size);
111   MIOBufferWriter bw(theMIOBuffer);
112 
113 #if 0
114   writeOnce(bw, 0);
115   writeOnce(bw, 1);
116   writeOnce(bw, 1);
117   writeOnce(bw, 1);
118   writeOnce(bw, 10);
119   writeOnce(bw, 1000);
120   writeOnce(bw, 1);
121   writeOnce(bw, 0);
122   writeOnce(bw, 1);
123   writeOnce(bw, 2000);
124   writeOnce(bw, 69);
125   writeOnce(bw, 666);
126 
127   std::cout << "Pre Loop" << std::endl;
128   for (int i = 0; i < 3000; i += 13) {
129     writeOnce(bw, i);
130   }
131   std::cout << "Post Loop" << std::endl;
132 
133   writeOnce(bw, 0);
134   writeOnce(bw, 1);
135 
136   REQUIRE(bw.extent() == 3000);
137 #endif
138 }
139