File: test-evaluate.c

package info (click to toggle)
hatari 2.4.1%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 13,840 kB
  • sloc: ansic: 161,233; python: 5,787; objc: 1,900; asm: 1,679; sh: 1,629; javascript: 145; makefile: 86; xml: 32
file content (95 lines) | stat: -rw-r--r-- 2,427 bytes parent folder | download | duplicates (3)
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
/* 
 * Code to test Hatari expression evaluation in src/debug/evaluate.c
 * (including Hatari variable and CPU register values in expressions)
 */
#include <stdio.h>
#include <SDL_types.h>
#include <stdbool.h>
#include "stMemory.h"
#include "evaluate.h"
#include "m68000.h"
#include "main.h"
#include "screen.h"
#include "configuration.h"
#include "video.h"

#define VBL_VALUE 21

int main(int argc, const char *argv[])
{
	/* expected to fail */
	const char *failure[] = {
		"1+2*",
		"*1+2",
		"1+(2",
		"1)+2",
		"foo+1+bar",
	};
	/* expected to succeed, with given result */
	struct {
		const char *expression;
		Uint32 result;
	} success[] = {
		{ "1+2*3", 7 },
		{ "(2+5)*3", 9 },
		{ "d0 + 2", 12 },
		{ "VBL+10", VBL_VALUE + 10 },
		{ "~%101 & $f0f0f ^ 0x21 * 0x200", 0xF4D0A },
	};
	int i, offset, tests = 0, errors = 0;
	const char *expression, *errstr;
	Uint32 result;

	/* set values needed by above successful calculations */
	nVBLs = VBL_VALUE;
	memset(Regs, 0, sizeof(Regs));
	Regs[REG_D0] = 10;
	memset(STRam, 0, STRamEnd);
	/* expressions use long access, 3*3 -> 9 */
	STMemory_WriteLong(2+5, 3);

	fprintf(stderr, "\nExpressions that should FAIL:\n");

	for (i = 0; i < ARRAY_SIZE(failure); i++) {
		expression = failure[i];
		fprintf(stderr, "- '%s'\n", expression);
		errstr = Eval_Expression(expression, &result, &offset, false);
		if (errstr) {
			fprintf(stderr, "%*c-%s\n",
				3+offset, '^', errstr);
		} else {
			fprintf(stderr, "  => %x\n  ***Unexpected SUCCESS from expression***\n",
				(Uint32)result);
			errors++;
		}
	}
	tests += i;

	fprintf(stderr, "\nExpressions that should SUCCEED with given result:\n");

	for (i = 0; i < ARRAY_SIZE(success); i++) {
		expression = success[i].expression;
		fprintf(stderr, "- '%s'\n", expression);
		errstr = Eval_Expression(expression, &result, &offset, false);
		if (errstr) {
			fprintf(stderr, "%*c-%s\n  ***Unexpected ERROR in expression***\n",
				3+offset, '^', errstr);
			errors++;
		} else if (result != success[i].result) {
			fprintf(stderr, "  => %x (not %x)\n  ***Wrong result from expression***\n",
				(Uint32)result, (Uint32)success[i].result);
			errors++;
		} else {
			fprintf(stderr, "  => 0x%x\n", (Uint32)result);
		}
	}
	tests += i;

	if (errors) {
		fprintf(stderr, "\n***Detected %d ERRORs in %d automated tests!***\n\n",
			errors, tests);
	} else {
		fprintf(stderr, "\nFinished without any errors!\n\n");
	}
	return errors;
}