Ver código fonte

Fix bug in buffer.c, add buffer mini-unit test.

tags/v0.9.3
Michael Brown 18 anos atrás
pai
commit
c0ef730ec4
2 arquivos alterados com 57 adições e 3 exclusões
  1. 3
    3
      src/core/buffer.c
  2. 54
    0
      src/tests/buffertest.c

+ 3
- 3
src/core/buffer.c Ver arquivo

@@ -80,21 +80,21 @@ struct buffer_free_block {
80 80
  * @ret block		Next free block descriptor
81 81
  * @ret rc		Return status code
82 82
  *
83
- * Set @c block->next=buffer->free before first call to
83
+ * Set @c block->next=buffer->fill before first call to
84 84
  * get_next_free_block().
85 85
  */
86 86
 static int get_next_free_block ( struct buffer *buffer,
87 87
 				 struct buffer_free_block *block ) {
88 88
 
89 89
 	/* Check for end of buffer */
90
-	if ( block->end >= buffer->len )
90
+	if ( block->next >= buffer->len )
91 91
 		return -ENOENT;
92 92
 
93 93
 	/* Move to next block */
94 94
 	block->start = block->next;
95 95
 	if ( block->start >= buffer->free ) {
96 96
 		/* Final block; no in-band descriptor */
97
-		block->end = buffer->len;
97
+		block->next = block->end = buffer->len;
98 98
 	} else {
99 99
 		/* Retrieve block descriptor */
100 100
 		copy_from_phys ( block, ( buffer->addr + block->start ),

+ 54
- 0
src/tests/buffertest.c Ver arquivo

@@ -0,0 +1,54 @@
1
+#include <assert.h>
2
+#include <gpxe/buffer.h>
3
+
4
+
5
+struct buffer_test {
6
+	struct buffer buffer;
7
+	const char *source;
8
+	size_t source_len;
9
+	char *dest;
10
+	size_t dest_len;
11
+};
12
+
13
+static int test_fill_buffer ( struct buffer_test *test,
14
+			      size_t start, size_t end ) {
15
+	const void *data = ( test->source + start );
16
+	size_t len = ( end - start );
17
+
18
+	assert ( end <= test->source_len );
19
+	assert ( end <= test->dest_len );
20
+
21
+	fill_buffer ( &test->buffer, data, start, len );
22
+	assert ( memcmp ( ( test->dest + start ), data, len ) == 0 );
23
+	assert ( test->buffer.free >= end );
24
+	return 0;
25
+}
26
+
27
+int test_buffer ( void ) {
28
+	char source[123];
29
+	char dest[123];
30
+	struct buffer_test test;
31
+
32
+	memset ( &test, 0, sizeof ( test ) );
33
+	test.source = source;
34
+	test.source_len = sizeof ( source );
35
+	test.dest = dest;
36
+	test.dest_len = sizeof ( dest );
37
+	test.buffer.addr = virt_to_phys ( dest );
38
+	test.buffer.len = sizeof ( dest );
39
+
40
+	test_fill_buffer ( &test,  20,  38 );
41
+	test_fill_buffer ( &test,  60,  61 );
42
+	test_fill_buffer ( &test,  38,  42 );
43
+	test_fill_buffer ( &test,  42,  60 );
44
+	test_fill_buffer ( &test,  16,  80 );
45
+	test_fill_buffer ( &test,   0,  16 );
46
+	test_fill_buffer ( &test,  99, 123 );
47
+	test_fill_buffer ( &test,  80,  99 );
48
+
49
+	assert ( test.buffer.fill == sizeof ( source ) );
50
+	assert ( test.buffer.free == sizeof ( source ) );
51
+	assert ( memcmp ( source, dest, sizeof ( source ) ) == 0 );
52
+
53
+	return 0;
54
+}

Carregando…
Cancelar
Salvar