-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathTestDiskQueue.cpp
More file actions
172 lines (128 loc) · 4.58 KB
/
Copy pathTestDiskQueue.cpp
File metadata and controls
172 lines (128 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "TestUtil.h"
#include "plotting/DiskQueue.h"
#include "plotting/DiskBucketBuffer.h"
#include "plotting/DiskBuffer.h"
#include "util/VirtualAllocator.h"
constexpr uint32 bucketCount = 64;
constexpr uint32 entriesPerBucket = 1 << 16;
constexpr uint32 entriesPerSlice = entriesPerBucket / bucketCount;
static void WriteBucketSlices( DiskBucketBuffer* buf, uint32 bucket, uint32 mask, Span<size_t> sliceSizes );
//-----------------------------------------------------------
TEST_CASE( "disk-slices", "[disk-queue]" )
{
const char* tempPath = GetEnv( "bb_queue_path", "/Users/harito/.sandbox/plot" );
DiskQueue queue( tempPath );
auto buf = std::unique_ptr<DiskBucketBuffer>( DiskBucketBuffer::Create(
queue, "slices.tmp", bucketCount, sizeof( uint32 ) * entriesPerSlice,
FileMode::Create, FileAccess::ReadWrite ) );
ENSURE( buf.get() );
{
VirtualAllocator allocator{};
buf->ReserveBuffers( allocator );
}
size_t _sliceSizes[bucketCount] = {};
for( uint32 i = 0; i < bucketCount; i++ )
_sliceSizes[i] = entriesPerSlice * sizeof( uint32 );
Span<size_t> sliceSizes( _sliceSizes, bucketCount );
// Write a whole "table"'s worth of data
for( uint32 b = 0; b < bucketCount; b++ )
{
WriteBucketSlices( buf.get(), b, 0, sliceSizes );
}
// Read back
buf->Swap();
const uint32 secondMask = 0xF0000000;
{
buf->ReadNextBucket();
for( uint32 b = 0; b < bucketCount; b++ )
{
buf->TryReadNextBucket();
auto input = buf->GetNextReadBufferAs<uint32>();
const uint32 readMask = b << 16;
ENSURE( input.Length() == entriesPerBucket );
// Validate
for( uint32 i = 0; i < input.Length(); i++ )
{
ENSURE( input[i] == (readMask | i ) );
}
// Write new bucket
WriteBucketSlices( buf.get(), b, secondMask, sliceSizes );
}
}
// Read again and validate the second match
buf->Swap();
{
buf->ReadNextBucket();
for( uint32 b = 0; b < bucketCount; b++ )
{
buf->TryReadNextBucket();
auto input = buf->GetNextReadBufferAs<uint32>();
const uint32 readMask = secondMask | (b << 16);
ENSURE( input.Length() == entriesPerBucket );
// Validate
for( uint32 i = 0; i < input.Length(); i++ )
{
ENSURE( input[i] == (readMask | i ) );
}
}
}
buf->Swap();
Log::Line( "Ok" );
}
//-----------------------------------------------------------
TEST_CASE( "disk-buckets", "[disk-queue]" )
{
const char* tempPath = GetEnv( "bb_queue_path", "/Users/harito/.sandbox/plot" );
DiskQueue queue( tempPath );
auto buffer = std::unique_ptr<DiskBuffer>( DiskBuffer::Create(
queue, "bucket.tmp",
bucketCount, sizeof( uint32 ) * entriesPerBucket,
FileMode::Create, FileAccess::ReadWrite ) );
ENSURE( buffer );
{
VirtualAllocator allocator{};
buffer->ReserveBuffers( allocator );
}
// Write bucket
{
for( uint32 b = 0; b < bucketCount; b++ )
{
auto bucket = buffer->GetNextWriteBufferAs<uint32>();
for( uint32 i = 0; i < entriesPerBucket; i++ )
bucket[i] = b * entriesPerBucket + i;
buffer->Submit( entriesPerBucket * sizeof( uint32 ) );
}
}
// Read back bucket
buffer->Swap();
{
buffer->ReadNextBucket();
for( uint32 b = 0; b < bucketCount; b++ )
{
buffer->TryReadNextBucket();
auto bucket = buffer->GetNextReadBufferAs<uint32>();
// Validate
ENSURE( bucket.Length() == entriesPerBucket );
for( uint32 i = 0; i < entriesPerBucket; i++ )
{
ENSURE( bucket[i] == b * entriesPerBucket + i );
}
}
}
Log::Line( "Ok" );
}
//-----------------------------------------------------------
void WriteBucketSlices( DiskBucketBuffer* buf, uint32 bucket, uint32 writeMask, Span<size_t> sliceSizes )
{
const uint32 base = entriesPerSlice * bucket;
auto slices = buf->GetNextWriteBufferAs<uint32>();
for( uint32 slice = 0; slice < bucketCount; slice++ )
{
const uint32 mask = writeMask | (slice << 16);
for( uint32 i = 0; i < entriesPerSlice; i++ )
slices[i] = mask | (base + i);
slices = slices.Slice( buf->GetSliceStride() / sizeof( uint32 ) );
}
// Submit next buffer
buf->Submit();
}