-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_op.cpp
122 lines (115 loc) · 5.61 KB
/
test_op.cpp
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
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cstdarg>
#include <pthread.h>
#include <semaphore.h>
#include "common.h"
#include "test_op.h"
using namespace std;
static int g_Fail;
static pthread_mutex_t g_Mtx;
//-------------------------------------------------------------------------------------------------
void reportError ( const char * prefix,
... )
{
va_list va;
pthread_mutex_lock ( &g_Mtx );
g_Fail ++;
va_start ( va, prefix );
vprintf (prefix, va );
va_end ( va );
pthread_mutex_unlock ( &g_Mtx );
}
//-------------------------------------------------------------------------------------------------
void checkResize ( CCPU * cpu,
uint32_t limit )
{
cpu -> SetMemLimit ( limit );
if ( cpu -> GetMemLimit () != limit )
reportError ( "Mem limit error: %d expected, %d returned\n", limit, cpu -> GetMemLimit () );
}
//-------------------------------------------------------------------------------------------------
static void checkWrite ( CCPU * cpu,
uint32_t addr,
uint32_t val )
{
if ( ! cpu -> WriteInt ( addr, val ) )
reportError ( "failed: WriteInt ( %x, %u )\n", addr, val );
}
//-------------------------------------------------------------------------------------------------
static void checkRead ( CCPU * cpu,
uint32_t addr,
uint32_t expected )
{
uint32_t val;
if ( ! cpu -> ReadInt ( addr, val ) )
reportError ( "failed: ReadInt ( %x, ... )\n", addr );
else if ( val != expected )
reportError ( "read mismatch, addr %x, read: %u, expected: %u\n", addr, val, expected );
}
//-------------------------------------------------------------------------------------------------
void wTest ( CCPU * cpu,
uint32_t fromPage,
uint32_t toPage )
{
for ( uint32_t addr = fromPage * CCPU::PAGE_SIZE; addr < toPage * CCPU::PAGE_SIZE; addr += 32 )
checkWrite ( cpu, addr, addr ^ fromPage );
}
//-------------------------------------------------------------------------------------------------
void rTest ( CCPU * cpu,
uint32_t fromPage,
uint32_t toPage )
{
for ( uint32_t addr = fromPage * CCPU::PAGE_SIZE; addr < toPage * CCPU::PAGE_SIZE; addr += 32 )
checkRead ( cpu, addr, addr ^ fromPage );
}
//-------------------------------------------------------------------------------------------------
void iTest ( CCPU * cpu,
uint32_t pages )
{
uint32_t val;
for ( uint32_t i = pages * CCPU::PAGE_SIZE; i < 4 * pages * CCPU::PAGE_SIZE; i += 744 )
if ( cpu -> ReadInt ( i, val ) )
reportError ( "readInt (%d) succeeds, shall fail\n", i );
for ( uint32_t i = pages * CCPU::PAGE_SIZE; i < 4 * pages * CCPU::PAGE_SIZE; i += 952 )
if ( cpu -> WriteInt ( i, i - 300 ) )
reportError ( "writeInt (%d) succeeds, shall fail\n", i );
for ( uint32_t i = pages * CCPU::PAGE_SIZE; i < 0xfff00000; i += 500000 )
if ( cpu -> ReadInt ( i, val ) )
reportError ( "readInt (%d) succeeds, shall fail\n", i );
for ( uint32_t i = pages * CCPU::PAGE_SIZE; i < 0xfff00000; i += 500000 )
if ( cpu -> WriteInt ( i, i - 300 ) )
reportError ( "writeInt (%d) succeeds, shall fail\n", i );
}
//-------------------------------------------------------------------------------------------------
void rwTest ( CCPU * cpu,
uint32_t fromPage,
uint32_t toPage )
{
wTest ( cpu, fromPage, toPage );
rTest ( cpu, fromPage, toPage );
}
//-------------------------------------------------------------------------------------------------
void rwiTest ( CCPU * cpu,
uint32_t fromPage,
uint32_t toPage )
{
wTest ( cpu, fromPage, toPage );
rTest ( cpu, fromPage, toPage );
iTest ( cpu, toPage );
}
//-------------------------------------------------------------------------------------------------
void testStart ( void )
{
pthread_mutex_init ( &g_Mtx, NULL );
g_Fail = 0;
}
//-------------------------------------------------------------------------------------------------
void testEnd ( const char * prefix )
{
pthread_mutex_destroy ( &g_Mtx );
printf ( "%s: %d failures\n", prefix, g_Fail );
}
//-------------------------------------------------------------------------------------------------