1
+ // ===-- flang/unittests/RuntimeGTest/BufferTest.cpp -------------*- C++ -*-===//
2
+ //
3
+ // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
+ // See https://llvm.org/LICENSE.txt for license information.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
+ //
7
+ // ===----------------------------------------------------------------------===//
8
+
1
9
#include " ../../runtime/buffer.h"
2
- #include " testing.h"
10
+ #include " CrashHandlerFixture.h"
11
+ #include " gtest/gtest.h"
3
12
#include < algorithm>
4
13
#include < cstdint>
5
14
#include < cstring>
6
15
#include < memory>
7
16
8
- static constexpr std::size_t tinyBuffer {32 };
17
+ static constexpr std::size_t tinyBufferSize {32 };
9
18
using FileOffset = std::int64_t ;
10
19
using namespace Fortran ::runtime;
11
20
using namespace Fortran ::runtime::io;
12
21
13
- class Store : public FileFrame <Store, tinyBuffer > {
22
+ class Store : public FileFrame <Store, tinyBufferSize > {
14
23
public:
15
24
explicit Store (std::size_t bytes = 65536 ) : bytes_{bytes} {
16
25
data_.reset (new char [bytes]);
@@ -60,16 +69,17 @@ class Store : public FileFrame<Store, tinyBuffer> {
60
69
61
70
inline int ChunkSize (int j, int most) {
62
71
// 31, 1, 29, 3, 27, ...
63
- j %= tinyBuffer ;
64
- auto chunk{
65
- static_cast < int >((( j % 2 ) ? j : (tinyBuffer - 1 - j)) % tinyBuffer )};
72
+ j %= tinyBufferSize ;
73
+ auto chunk{static_cast < int >(
74
+ (( j % 2 ) ? j : (tinyBufferSize - 1 - j)) % tinyBufferSize )};
66
75
return std::min (chunk, most);
67
76
}
68
77
69
78
inline int ValueFor (int at) { return (at ^ (at >> 8 )) & 0xff ; }
70
79
71
- int main () {
72
- StartTests ();
80
+ struct BufferTests : CrashHandlerFixture {};
81
+
82
+ TEST (BufferTests, TestFrameBufferReadAndWrite) {
73
83
Terminator terminator{__FILE__, __LINE__};
74
84
IoErrorHandler handler{terminator};
75
85
Store store;
@@ -94,22 +104,19 @@ int main() {
94
104
while (at < bytes) {
95
105
auto chunk{ChunkSize (j, static_cast <int >(bytes - at))};
96
106
std::size_t frame{store.ReadFrame (at, chunk, handler)};
97
- if (frame < static_cast <std::size_t >(chunk)) {
98
- Fail () << " Badly-sized ReadFrame at " << at << " , chunk=" << chunk
99
- << " , got " << frame << ' \n ' ;
100
- break ;
101
- }
107
+ ASSERT_GE (frame, static_cast <std::size_t >(chunk))
108
+ << " Badly-sized ReadFrame at " << at << " , chunk=" << chunk << " , got "
109
+ << frame << ' \n ' ;
110
+
102
111
const char *from{store.Frame ()};
103
112
for (int k{0 }; k < chunk; ++k) {
104
113
auto expect{static_cast <char >(ValueFor (at + k))};
105
- if (from[k] != expect) {
106
- Fail () << " At " << at << ' +' << k << ' (' << (at + k) << " ), read "
107
- << (from[k] & 0xff ) << " , expected " << static_cast <int >(expect)
108
- << ' \n ' ;
109
- }
114
+ ASSERT_EQ (from[k], expect)
115
+ << " At " << at << ' +' << k << ' (' << (at + k) << " ), read "
116
+ << (from[k] & 0xff ) << " , expected " << static_cast <int >(expect)
117
+ << ' \n ' ;
110
118
}
111
119
at += chunk;
112
120
++j;
113
121
}
114
- return EndTests ();
115
122
}
0 commit comments