|
| 1 | +to_test_case :: (canonical_case: CanonicalCase) -> Test { |
| 2 | + using test: Test; |
| 3 | + name = to_test_name(canonical_case.description); |
| 4 | + skip = canonical_case.index > 0; |
| 5 | + _, capacity := get_property(canonical_case.input, "capacity"); |
| 6 | + _, operations := get_array_property(canonical_case.input, "operations"); |
| 7 | + |
| 8 | + lines: [..]string; |
| 9 | + array_add(*lines, tprint("CAPACITY :: %;", to_code(capacity))); |
| 10 | + array_add(*lines, "circular_buffer := new_circular_buffer(CAPACITY);"); |
| 11 | + |
| 12 | + write_idx, read_idx: int = 1; |
| 13 | + |
| 14 | + for operation: operations { |
| 15 | + _, operation_name := get_string_property(*operation, "operation"); |
| 16 | + _, should_succeed := get_boolean_property(*operation, "should_succeed"); |
| 17 | + |
| 18 | + if operation_name == { |
| 19 | + case "write"; |
| 20 | + _, item := get_number_property(*operation, "item"); |
| 21 | + array_add(*lines, tprint("write_successful_% := write(*circular_buffer, %);", write_idx, to_code(item))); |
| 22 | + array_add(*lines, tprint("assert_equal(%, write_successful_%);", to_code(should_succeed), write_idx)); |
| 23 | + write_idx += 1; |
| 24 | + case "read"; |
| 25 | + array_add(*lines, tprint("read_successful_%, read_value_% := read(*circular_buffer);", read_idx, read_idx)); |
| 26 | + array_add(*lines, tprint("assert_equal(%, read_successful_%);", to_code(should_succeed), read_idx)); |
| 27 | + if should_succeed { |
| 28 | + _, expected := get_number_property(*operation, "expected"); |
| 29 | + array_add(*lines, tprint("assert_equal(%, read_value_%);", to_code(expected), read_idx)); |
| 30 | + } |
| 31 | + read_idx += 1; |
| 32 | + case "clear"; |
| 33 | + array_add(*lines, "clear(*circular_buffer);"); |
| 34 | + case "overwrite"; |
| 35 | + _, item := get_number_property(*operation, "item"); |
| 36 | + array_add(*lines, tprint("overwrite(*circular_buffer, %);", to_code(item))); |
| 37 | + case; |
| 38 | + assert(false, tprint("Unexpected operation: %", operation_name)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + test.multiline_body = lines; |
| 43 | + |
| 44 | + return test; |
| 45 | +} |
| 46 | + |
| 47 | +#run run_generator("circular-buffer", to_test_case); |
| 48 | + |
| 49 | +#import "Basic"; |
| 50 | +#import,dir "../../../shared/generator"; |
0 commit comments