| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn test_string_cursor_tracks_character_boundaries_and_rejects_invalid_indices() { |
| 6 | value := string_value('aλ😀中é\x00z') |
| 7 | text := value.string_pointer() |
| 8 | expected := [0, 1, 3, 7, 10, 12, 13, 14] |
| 9 | for _ in 0 .. 4 { |
| 10 | for i, offset in expected { assert text_offset(text, i)! == offset } |
| 11 | for i := expected.len - 1; i >= 0; i-- { assert text_offset(text, i)! == expected[i] } |
| 12 | for i in [3, 1, 6, 2, 5, 0, 4] { assert text_offset(text, i)! == expected[i] } |
| 13 | } |
| 14 | for index in [i64(-1), i64(expected.len), i64(exact_max)] { |
| 15 | if _ := text_offset(text, index) { assert false } |
| 16 | } |
| 17 | assert text_offset(text, 2)! == 3 |
| 18 | assert text.revision == 0 |
| 19 | } |
| 20 | |
| 21 | fn test_string_cursor_invalidates_on_in_place_edits_and_width_changes_in_both_frontends() { |
| 22 | for alternate in [false, true] { |
| 23 | mut vm := new() |
| 24 | target := string_value('λλλλ') |
| 25 | vm.define('s', target)! |
| 26 | for alias, name in {'ref': 'string-ref', 'copy': 'string-copy!', 'fill': 'string-fill!', 'put': 'string-set!'} { |
| 27 | vm.define(alias, vm.lookup(name)!)! |
| 28 | } |
| 29 | vm.define('narrow', character(`é`)!)! |
| 30 | vm.define('wide', character(`😀`)!)! |
| 31 | assert vm.call(vm.lookup('ref')!, [target, integer(2)])!.as_character()! == `λ` |
| 32 | // This replaces six bytes with six bytes but changes internal boundaries. |
| 33 | if alternate { vm.eval_alt('copy(s, 0, "😀ab")')! } |
| 34 | else { vm.eval('(copy s 0 "😀ab")')! } |
| 35 | assert vm.call(vm.lookup('ref')!, [target, integer(2)])!.as_character()! == `b` |
| 36 | assert target.text() == '😀abλ' |
| 37 | if alternate { vm.eval_alt('fill(s, narrow, 0, 3)')! } |
| 38 | else { vm.eval('(fill s narrow 0 3)')! } |
| 39 | assert vm.call(vm.lookup('ref')!, [target, integer(2)])!.as_character()! == `é` |
| 40 | if alternate { vm.eval_alt('put(s, 0, wide)')! } |
| 41 | else { vm.eval('(put s 0 wide)')! } |
| 42 | assert vm.call(vm.lookup('ref')!, [target, integer(3)])!.as_character()! == `λ` |
| 43 | assert target.text() == '😀ééλ' |
| 44 | assert target.string_pointer().length == 4 |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn test_string_cursor_observes_input_into_existing_aliases_and_keeps_snapshots_owned() { |
| 49 | mut vm := new() |
| 50 | target := string_value('λλλλ') |
| 51 | snapshot := target.as_string()! |
| 52 | assert text_offset(target.string_pointer(), 2)! == 4 |
| 53 | port := vm.open_input_string('😀a')! |
| 54 | assert vm.read_string_into(port, target, 0, 2)!.whole() == 2 |
| 55 | assert target.text() == '😀aλλ' |
| 56 | assert text_offset(target.string_pointer(), 2)! == 5 |
| 57 | assert text_offset(target.string_pointer(), 3)! == 7 |
| 58 | assert snapshot == 'λλλλ' |
| 59 | vm.close_port(port)! |
| 60 | } |
| 61 | |
| 62 | fn test_string_cursor_lookup_uses_no_operation_allocation() { |
| 63 | mut region := arena.new() |
| 64 | region.push() |
| 65 | defer { region.pop(); region.free() } |
| 66 | value := string_value('λ😀a'.repeat(100)) |
| 67 | text := value.string_pointer() |
| 68 | before := region.used() |
| 69 | for _ in 0 .. 1000 { |
| 70 | for i in 0 .. 100 { |
| 71 | assert text_offset(text, 3 * i)! == 7 * i |
| 72 | assert text_offset(text, 3 * i + 1)! == 7 * i + 2 |
| 73 | assert text_offset(text, 3 * i + 2)! == 7 * i + 6 |
| 74 | } |
| 75 | } |
| 76 | assert region.used() == before |
| 77 | } |
| 78 | |
| 79 | fn cursor_readonly_worker(text &StringData, phase int) bool { |
| 80 | expected := [0, 1, 3, 7, 10, 12] |
| 81 | for i in 0 .. 20000 { |
| 82 | index := (i * 5 + phase) % expected.len |
| 83 | offset := text_offset(text, index) or { return false } |
| 84 | if offset != expected[index] { return false } |
| 85 | } |
| 86 | return true |
| 87 | } |
| 88 | |
| 89 | fn test_string_cursor_readonly_readers_share_a_consistent_position() { |
| 90 | value := string_value('aλ😀中é') |
| 91 | first := spawn cursor_readonly_worker(value.string_pointer(), 0) |
| 92 | second := spawn cursor_readonly_worker(value.string_pointer(), 3) |
| 93 | assert first.wait() |
| 94 | assert second.wait() |
| 95 | assert value.text() == 'aλ😀中é' |
| 96 | } |
| 97 | |