summaryrefslogtreecommitdiffstats
path: root/lib/libUPnP/Neptune/Source/Tests/RingBuffer1/RingBufferTest1.cpp
blob: 43471824a6ace0bcb4a7f9d3e20fccd6d1d6fac3 (plain)
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
/*****************************************************************
|
|      RingBuffer Test Program 1
|
|      (c) 2001-2005 Gilles Boccon-Gibod
|      Author: Gilles Boccon-Gibod (bok@bok.net)
|
 ****************************************************************/

/*----------------------------------------------------------------------
|       includes
+---------------------------------------------------------------------*/
#include "Neptune.h"
#include <stdlib.h>

#if defined(WIN32)
#include <crtdbg.h>
#endif

const unsigned int BUFFER_SIZE = 17;

/*----------------------------------------------------------------------
|       ReadChunk
+---------------------------------------------------------------------*/
static NPT_Result
ReadChunk(NPT_RingBuffer& buffer)
{
    static unsigned int total_read = 0;
    unsigned int chunk = rand()%BUFFER_SIZE;
    unsigned int can_read = buffer.GetAvailable();
    if (chunk > can_read) chunk = can_read;
    if (chunk == 0) return NPT_SUCCESS;

    // read a chunk
    unsigned char bytes[BUFFER_SIZE];
    NPT_CHECK(buffer.Read(bytes, chunk));

    // check values
    for (unsigned int i=0; i<chunk; i++) {
        unsigned int index = total_read+i;
        unsigned char expected = index & 0xFF;
        if (bytes[i] != expected) {
            printf("unexpected byte at index %d (expected %d, got %d)\n", 
                   index, expected, bytes[i]);
            return NPT_FAILURE;
        }
    }
    total_read += chunk;

    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|       WriteChunk
+---------------------------------------------------------------------*/
static NPT_Result
WriteChunk(NPT_RingBuffer& buffer)
{
    static unsigned int total_written = 0;
    unsigned int chunk = rand()%BUFFER_SIZE;
    unsigned int can_write = buffer.GetSpace();
    if (chunk > can_write) chunk = can_write;
    if (chunk == 0) return NPT_SUCCESS;

    // generate buffer
    unsigned char bytes[BUFFER_SIZE];
    for (unsigned int i=0; i<chunk; i++) {
        unsigned int index = total_written+i;
        bytes[i] = index&0xFF;
    }

    // write chunk
    NPT_CHECK(buffer.Write(bytes, chunk));
    total_written += chunk;

    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|       main
+---------------------------------------------------------------------*/
int
main(int /*argc*/, char** /*argv*/)
{
    // setup debugging
#if defined(WIN32) && defined(_DEBUG)
    int flags = _crtDbgFlag       | 
        _CRTDBG_ALLOC_MEM_DF      |
        _CRTDBG_DELAY_FREE_MEM_DF |
        _CRTDBG_CHECK_ALWAYS_DF;

    _CrtSetDbgFlag(flags);
    //AllocConsole();
    //freopen("CONOUT$", "w", stdout);
#endif 

    NPT_RingBuffer buffer(BUFFER_SIZE);
    
    for (int i=0; i<100000000; i++) {
        if (NPT_FAILED(WriteChunk(buffer))) {
            printf("WriteChunk failed\n");
            return 1;
        }
        if (NPT_FAILED(ReadChunk(buffer))) {
            printf("ReadChunk failed\n");
            return 1;
        }
    }

    printf("RingBufferTest1 passed\n");

    return 0;
}