about summary refs log tree commit diff stats
path: root/archive/2.vm/002test.cc
blob: 817b0d47c806bcf1f3ee56b6c18e12e55c24f72a (plain) (blame)
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
//: A simple test harness. To create new tests, define functions starting with
//: 'test_'. To run all tests so defined, run:
//:   $ ./mu test
//:
//: Every layer should include tests, and can reach into previous layers.
//: However, it seems like a good idea never to reach into tests from previous
//: layers. Every test should be a contract that always passes as originally
//: written, regardless of any later layers. Avoid writing 'temporary' tests
//: that are only meant to work until some layer.

:(before "End Types")
typedef void (*test_fn)(void);
:(before "Globals")
// move a global ahead into types that we can't generate an extern declaration for
const test_fn Tests[] = {
  #include "test_list"  // auto-generated; see 'build*' scripts
};

:(before "End Globals")
bool Run_tests = false;
bool Passed = true;  // set this to false inside any test to indicate failure

:(before "End Includes")
#define CHECK(X) \
  if (Passed && !(X)) { \
    cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << '\n'; \
    Passed = false; \
    return;  /* Currently we stop at the very first failure. */ \
  }

#define CHECK_EQ(X, Y) \
  if (Passed && (X) != (Y)) { \
    cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << " == " << #Y << '\n'; \
    cerr << "  got " << (X) << '\n';  /* BEWARE: multiple eval */ \
    Passed = false; \
    return;  /* Currently we stop at the very first failure. */ \
  }

:(before "End Reset")
Passed = true;

:(before "End Commandline Parsing")
if (argc > 1 && is_equal(argv[1], "test")) {
  Run_tests = true;  --argc;  ++argv;  // shift 'test' out of commandline args
}

:(before "End Main")
if (Run_tests) {
  // Test Runs
  // we run some tests and then exit; assume no state need be maintained afterward

  long num_failures = 0;
  // End Test Run Initialization
  time_t t;  time(&t);
  cerr << "C tests: " << ctime(&t);
  for (size_t i=0;  i < sizeof(Tests)/sizeof(Tests[0]);  ++i) {
//?     cerr << "running " << Test_names[i] << '\n';
    run_test(i);
    if (Passed) cerr << '.';
    else ++num_failures;
  }
  cerr << '\n';
  // End Tests
  if (num_failures > 0) {
    cerr << num_failures << " failure"
         << (num_failures > 1 ? "s" : "")
         << '\n';
    return 1;
  }
  return 0;
}

:(code)
void run_test(size_t i) {
  if (i >= sizeof(Tests)/sizeof(Tests[0])) {
    cerr << "no test " << i << '\n';
    return;
  }
  reset();
  // End Test Setup
  (*Tests[i])();
  // End Test Teardown
}

//: Convenience: run a single test
:(before "Globals")
// Names for each element of the 'Tests' global, respectively.
const string Test_names[] = {
  #include "test_name_list"  // auto-generated; see 'build*' scripts
};
:(after "Test Runs")
string maybe_single_test_to_run = argv[argc-1];
if (!starts_with(maybe_single_test_to_run, "test_"))
  maybe_single_test_to_run.insert(0, "test_");
for (size_t i=0;  i < sizeof(Tests)/sizeof(Tests[0]);  ++i) {
  if (Test_names[i] == maybe_single_test_to_run) {
    run_test(i);
    if (Passed) cerr << ".\n";
    return 0;
  }
}

:(before "End Includes")
#include <stdlib.h>
n class="p">><span class="subxComment"># instruction effective address register displacement immediate</span> <span id="L11" class="LineNr">11 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> <span id="L12" class="LineNr">12 </span><span class="subxS1Comment"># . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes</span> <span id="L13" class="LineNr">13 </span> <span id="L14" class="LineNr">14 </span><span class="SpecialChar">Entry</span>: <span id="L15" class="LineNr">15 </span> <span class="subxComment"># mmap(Mmap-new-segment-&gt;len)</span> <span id="L16" class="LineNr">16 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex12.subx.html#L29'>Mmap-new-segment</a></span>/imm32 <span id="L17" class="LineNr">17 </span> e8/call <a href='000init.subx.html#L71'>syscall_mmap</a>/disp32 <span id="L18" class="LineNr">18 </span> <span id="L19" class="LineNr">19 </span> <span class="subxComment"># write to *eax to check that we have access to the newly-allocated segment</span> <span id="L20" class="LineNr">20 </span> c7 0/subop/copy 0/mod/direct 0/rm32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x34/imm32 <span class="subxComment"># copy to *eax</span> <span id="L21" class="LineNr">21 </span> <span id="L22" class="LineNr">22 </span> <span class="subxComment"># exit(eax)</span> <span id="L23" class="LineNr">23 </span> 89/copy 3/mod/direct 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy eax to ebx</span> <span id="L24" class="LineNr">24 </span> e8/call <a href='000init.subx.html#L18'>syscall_exit</a>/disp32 <span id="L25" class="LineNr">25 </span> <span id="L26" class="LineNr">26 </span>== data <span id="L27" class="LineNr">27 </span> <span id="L28" class="LineNr">28 </span><span class="subxComment"># various constants used here were found in the Linux sources (search for file mman-common.h)</span> <span id="L29" class="LineNr">29 </span><span class="SpecialChar">Mmap-new-segment</span>: <span class="subxComment"># type mmap_arg_struct</span> <span id="L30" class="LineNr">30 </span> <span class="subxComment"># addr</span> <span id="L31" class="LineNr">31 </span> 0/imm32 <span id="L32" class="LineNr">32 </span> <span class="subxComment"># len</span> <span id="L33" class="LineNr">33 </span> 0x100/imm32 <span id="L34" class="LineNr">34 </span> <span class="subxComment"># protection flags</span> <span id="L35" class="LineNr">35 </span> 3/imm32 <span class="subxComment"># PROT_READ | PROT_WRITE</span> <span id="L36" class="LineNr">36 </span> <span class="subxComment"># sharing flags</span> <span id="L37" class="LineNr">37 </span> 0x22/imm32 <span class="subxComment"># MAP_PRIVATE | MAP_ANONYMOUS</span> <span id="L38" class="LineNr">38 </span> <span class="subxComment"># fd</span> <span id="L39" class="LineNr">39 </span> -1/imm32 <span class="subxComment"># since MAP_ANONYMOUS is specified</span> <span id="L40" class="LineNr">40 </span> <span class="subxComment"># offset</span> <span id="L41" class="LineNr">41 </span> 0/imm32 <span class="subxComment"># since MAP_ANONYMOUS is specified</span> <span id="L42" class="LineNr">42 </span> <span id="L43" class="LineNr">43 </span><span class="subxS2Comment"># . . vim&#0058;nowrap:textwidth=0</span> </pre> </body> </html> <!-- vim: set foldmethod=manual : -->