about summary refs log tree commit diff stats
path: root/archive/1.vm/003trace.test.cc
blob: 30f61b5a2a4ab1a561652fc6a2b6128f1edd4827 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
void test_trace_check_compares() {
  trace("test layer") << "foo" << end();
  CHECK_TRACE_CONTENTS("test layer: foo");
}

void test_trace_check_ignores_other_layers() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 2") << "bar" << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
  CHECK_TRACE_DOESNT_CONTAIN("test layer 2: foo");
}

void test_trace_check_ignores_leading_whitespace() {
  trace("test layer 1") << " foo" << end();
  CHECK_EQ(trace_count("test layer 1", /*too little whitespace*/"foo"), 1);
  CHECK_EQ(trace_count("test layer 1", /*too much whitespace*/"  foo"), 1);
}

void test_trace_check_ignores_other_lines() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "bar" << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
}

void test_trace_check_ignores_other_lines2() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "bar" << end();
  CHECK_TRACE_CONTENTS("test layer 1: bar");
}

void test_trace_ignores_trailing_whitespace() {
  trace("test layer 1") << "foo\n" << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
}

void test_trace_ignores_trailing_whitespace2() {
  trace("test layer 1") << "foo " << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
}

void test_trace_orders_across_layers() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 2") << "bar" << end();
  trace("test layer 1") << "qux" << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo\n"
                       "test layer 2: bar\n"
                       "test layer 1: qux\n");
}

void test_trace_supports_count() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "foo" << end();
  CHECK_EQ(trace_count("test layer 1", "foo"), 2);
}

void test_trace_supports_count2() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "bar" << end();
  CHECK_EQ(trace_count("test layer 1"), 2);
}

void test_trace_count_ignores_trailing_whitespace() {
  trace("test layer 1") << "foo\n" << end();
  CHECK_EQ(trace_count("test layer 1", "foo"), 1);
}

// pending: DUMP tests
// pending: readable_contents() adds newline if necessary.
// pending: raise also prints to stderr.
// pending: raise doesn't print to stderr if Hide_errors is set.
// pending: raise doesn't have to be saved if Hide_errors is set, just printed.
// pending: raise prints to stderr if Trace_stream is NULL.
// pending: raise prints to stderr if Trace_stream is NULL even if Hide_errors is set.

// can't check trace because trace methods call 'split'

void test_split_returns_at_least_one_elem() {
  vector<string> result = split("", ",");
  CHECK_EQ(result.size(), 1);
  CHECK_EQ(result.at(0), "");
}

void test_split_returns_entire_input_when_no_delim() {
  vector<string> result = split("abc", ",");
  CHECK_EQ(result.size(), 1);
  CHECK_EQ(result.at(0), "abc");
}

void test_split_works() {
  vector<string> result = split("abc,def", ",");
  CHECK_EQ(result.size(), 2);
  CHECK_EQ(result.at(0), "abc");
  CHECK_EQ(result.at(1), "def");
}

void test_split_works2() {
  vector<string> result = split("abc,def,ghi", ",");
  CHECK_EQ(result.size(), 3);
  CHECK_EQ(result.at(0), "abc");
  CHECK_EQ(result.at(1), "def");
  CHECK_EQ(result.at(2), "ghi");
}

void test_split_handles_multichar_delim() {
  vector<string> result = split("abc,,def,,ghi", ",,");
  CHECK_EQ(result.size(), 3);
  CHECK_EQ(result.at(0), "abc");
  CHECK_EQ(result.at(1), "def");
  CHECK_EQ(result.at(2), "ghi");
}

void test_trim() {
  CHECK_EQ(trim(""), "");
  CHECK_EQ(trim(" "), "");
  CHECK_EQ(trim("  "), "");
  CHECK_EQ(trim("a"), "a");
  CHECK_EQ(trim(" a"), "a");
  CHECK_EQ(trim("  a"), "a");
  CHECK_EQ(trim("  ab"), "ab");
  CHECK_EQ(trim("a "), "a");
  CHECK_EQ(trim("a  "), "a");
  CHECK_EQ(trim("ab  "), "ab");
  CHECK_EQ(trim(" a "), "a");
  CHECK_EQ(trim("  a  "), "a");
  CHECK_EQ(trim("  ab  "), "ab");
}
s="w"> << maybe(caller.name) << "second ingredient of 'maybe-convert' should have type 'variant', but got " << inst.ingredients.at(1).original_string << '\n' << end(); break; } if (inst.products.empty()) break; reagent product = inst.products.at(0); if (!canonize_type(product)) break; reagent& offset = inst.ingredients.at(1); populate_value(offset); if (offset.value >= SIZE(get(Type, base.type->value).elements)) { raise_error << maybe(caller.name) << "invalid tag " << offset.value << " in '" << inst.to_string() << '\n' << end(); break; } reagent variant = variant_type(base, offset.value); variant.type = new type_tree(get(Type_ordinal, "address"), variant.type); if (!types_coercible(product, variant)) { raise_error << maybe(caller.name) << "'maybe-convert " << base.original_string << ", " << inst.ingredients.at(1).original_string << "' should write to " << debug_string(variant.type) << " but " << product.name << " has type " << debug_string(product.type) << '\n' << end(); break; } break; } :(before "End Primitive Recipe Implementations") case MAYBE_CONVERT: { reagent base = current_instruction().ingredients.at(0); canonize(base); long long int base_address = base.value; if (base_address == 0) { raise_error << maybe(current_recipe_name()) << "tried to access location 0 in '" << current_instruction().to_string() << "'\n" << end(); break; } long long int tag = current_instruction().ingredients.at(1).value; long long int result; if (tag == static_cast<long long int>(get_or_insert(Memory, base_address))) { result = base_address+1; } else { result = 0; } products.resize(1); products.at(0).push_back(result); break; } :(code) const reagent variant_type(const reagent& canonized_base, long long int tag) { assert(tag >= 0); assert(contains_key(Type, canonized_base.type->value)); assert(!get(Type, canonized_base.type->value).name.empty()); const type_info& info = get(Type, canonized_base.type->value); assert(info.kind == EXCLUSIVE_CONTAINER); reagent element; element.type = new type_tree(*info.elements.at(tag)); // End variant_type Special-cases return element; } :(scenario maybe_convert_product_type_mismatch) % Hide_errors = true; recipe main [ 12:number <- copy 1 13:number <- copy 35 14:number <- copy 36 20:address:number <- maybe-convert 12:number-or-point/unsafe, 1:variant ] +error: main: 'maybe-convert 12:number-or-point/unsafe, 1:variant' should write to <address : <point : <>>> but 20 has type <address : <number : <>>> //:: Allow exclusive containers to be defined in mu code. :(scenario exclusive_container) exclusive-container foo [ x:number y:number ] +parse: --- defining exclusive-container foo +parse: element name: x +parse: type: 1 +parse: element name: y +parse: type: 1 :(before "End Command Handlers") else if (command == "exclusive-container") { insert_container(command, EXCLUSIVE_CONTAINER, in); } //:: To construct exclusive containers out of variant types, use 'merge'. :(scenario lift_to_exclusive_container) exclusive-container foo [ x:number y:number ] recipe main [ 1:number <- copy 34 2:foo <- merge 0/x, 1:number # tag must be a literal when merging exclusive containers 4:foo <- merge 1/y, 1:number ] +mem: storing 0 in location 2 +mem: storing 34 in location 3 +mem: storing 1 in location 4 +mem: storing 34 in location 5 //: Since the different variants of an exclusive-container might have //: different sizes, relax the size mismatch check for 'merge' instructions. :(before "End size_mismatch(x) Cases") if (current_instruction().operation == MERGE && !current_instruction().products.empty() && current_instruction().products.at(0).type) { reagent x = current_instruction().products.at(0); canonize(x); if (get(Type, x.type->value).kind == EXCLUSIVE_CONTAINER) { return size_of(x) < SIZE(data); } } :(scenario merge_exclusive_container_with_mismatched_sizes) container foo [ x:number y:number ] exclusive-container bar [ x:number y:foo ] recipe main [ 1:number <- copy 34 2:number <- copy 35 3:bar <- merge 0/x, 1:number 6:bar <- merge 1/foo, 1:number, 2:number ] +mem: storing 0 in location 3 +mem: storing 34 in location 4 # bar is always 3 large so location 5 is skipped +mem: storing 1 in location 6 +mem: storing 34 in location 7 +mem: storing 35 in location 8