OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 #include <stdio.h> | |
6 #include <cinttypes> | |
7 | |
8 #define TESTING | |
9 #include "src/shared/assert.h" | |
10 #include "src/shared/atomic.h" | |
11 | |
12 #include "platforms/stm/disco_fletch/src/circular_buffer.h" | |
13 | |
14 void compare_buffers(uint8_t* buffer1, uint8_t* buffer2, int size) { | |
15 for (int i = 0; i < size; i++) { | |
16 EXPECT_EQ(buffer1[i], buffer2[i]); | |
17 } | |
18 } | |
19 | |
20 // This test will alternate between write and read, and always try to write | |
21 // 'to_write' bytes and read 'to_read' bytes. This will continue until 1000 | |
22 // bytes have been both written and read. | |
23 void write_read_test(int to_write, int to_read) { | |
24 const int size = 1000; | |
25 uint8_t* write_data = new uint8_t[size]; | |
26 uint8_t* read_data = new uint8_t[size]; | |
27 for (int i = 0; i < size; i++) { | |
28 write_data[i] = i % 0xff; | |
29 } | |
30 | |
31 const int buffer_size = 100; | |
32 CircularBuffer* cb = new CircularBuffer(buffer_size); | |
33 | |
34 for (int i = 0; i < size; i++) { | |
35 read_data[i] = 0; | |
36 } | |
37 | |
38 int buffer_content = 0; | |
39 int total_written = 0; | |
40 int total_read = 0; | |
41 while (total_written < size || total_read < size) { | |
42 int bytes = MIN(to_write, size - total_written); | |
43 int written = cb->Write( | |
44 write_data + total_written, bytes, CircularBuffer::kDontBlock); | |
45 EXPECT_LE(written, bytes); | |
46 EXPECT_LE(written, buffer_size - buffer_content); | |
47 total_written += written; | |
48 buffer_content += written; | |
49 EXPECT_LE(buffer_content, buffer_size); | |
50 if (buffer_content == buffer_size) { | |
51 EXPECT(cb->IsFull()); | |
52 EXPECT_EQ(0, cb->Write(write_data + total_written, | |
53 bytes, | |
54 CircularBuffer::kDontBlock)); | |
55 } | |
56 | |
57 int read = | |
58 cb->Read(read_data + total_read, to_read, CircularBuffer::kDontBlock); | |
59 EXPECT_LE(read, to_read); | |
60 EXPECT_LE(read, buffer_content); | |
61 total_read += read; | |
62 buffer_content -= read; | |
63 EXPECT_GE(buffer_content, 0); | |
64 if (buffer_content == 0) { | |
65 EXPECT(cb->IsEmpty()); | |
66 EXPECT_EQ(0, cb->Read(read_data + total_read, | |
67 to_read, | |
68 CircularBuffer::kDontBlock)); | |
69 } | |
70 } | |
71 compare_buffers(write_data, read_data, size); | |
72 } | |
73 | |
74 // This test will alternate between writing and reading the full | |
75 // capacity of the buffer. | |
76 void write_test() { | |
77 const int buffer_size = 100; | |
78 uint8_t* write_data = new uint8_t[buffer_size]; | |
79 uint8_t* read_data = new uint8_t[buffer_size]; | |
80 for (int i = 0; i < buffer_size; i++) { | |
81 write_data[i] = i % 0xff; | |
82 } | |
83 for (int i = 0; i < buffer_size; i++) { | |
84 read_data[i] = 0; | |
85 } | |
86 | |
87 CircularBuffer* cb = new CircularBuffer(buffer_size); | |
88 | |
89 for (int i = 0; i < buffer_size; i++) { | |
90 EXPECT_EQ(buffer_size, | |
91 cb->Write(write_data, buffer_size, CircularBuffer::kDontBlock)); | |
92 EXPECT(cb->IsFull()); | |
93 EXPECT_EQ(buffer_size, | |
94 cb->Read(read_data, buffer_size, CircularBuffer::kDontBlock)); | |
95 EXPECT(cb->IsEmpty()); | |
96 | |
97 compare_buffers(write_data, read_data, buffer_size); | |
98 } | |
99 } | |
100 | |
101 int main(int argc, char** argv) { | |
102 write_test(); | |
103 | |
104 for (int i = 1; i < 10; i++) { | |
105 for (int j = 1; j < 10; j++) { | |
106 write_read_test(i, j); | |
107 } | |
108 } | |
109 } | |
OLD | NEW |