about summary refs log tree commit diff stats
path: root/cpp/019address
blob: 540f18e6c5b3054651510e2a7bba0d74ac052e91 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//: Instructions can read from addresses pointing at other locations using the
//: 'deref' property.
:(scenario "copy_indirect")
recipe main [
  1:address:integer <- copy 2:literal
  2:integer <- copy 34:literal
  # This loads location 1 as an address and looks up *that* location.
  3:integer <- copy 1:address:integer/deref
]
+run: instruction main/2
+mem: location 1 is 2
+mem: location 2 is 34
+mem: storing in location 3

:(replace{} "vector<int> read_memory(reagent x)")
vector<int> read_memory(reagent x) {
  vector<int> result;
  if (x.types[0] == 0) {  // literal
    result.push_back(x.value);
    return result;
  }
  x = canonize(x);
  int base = x.value;
  size_t size = size_of(x);
  for (size_t offset = 0; offset < size; ++offset) {
    int val = Memory[base+offset];
    trace("mem") << "location " << base+offset << " is " << val;
    result.push_back(val);
  }
  return result;
}

//: similarly, write to addresses pointing at other locations using the
//: 'deref' property
:(scenario "store_indirect")
recipe main [
  1:address:integer <- copy 2:literal
  1:address:integer/deref <- copy 34:literal
]
+run: instruction main/1
+mem: location 1 is 2
+mem: storing in location 2

:(replace{} "void write_memory(reagent x, vector<int> data)")
void write_memory(reagent x, vector<int> data) {
  if (x.name == "_") return;  // dummy results are never stored
  x = canonize(x);
  int base = x.value;
  if (!Type[x.types[0]].is_array && size_of(x) != data.size())
    raise << "size mismatch in storing to " << x.to_string();
  for (size_t offset = 0; offset < data.size(); ++offset) {
    trace("mem") << "storing in location " << base+offset;
    Memory[base+offset] = data[offset];
  }
}

:(code)
reagent canonize(reagent x) {
  reagent r = x;
  while (has_property(r, "deref"))
    r = deref(r);
  return r;
}

bool has_property(reagent x, string name) {
  for (size_t i = 0; i < x.properties.size(); ++i) {
    if (x.properties[i].first == name) return true;
  }
  return false;
}

reagent deref(reagent x) {
  reagent result("");
  assert(x.types[0] == 2);  // address

  // compute value
  result.value = Memory[x.value];
  trace("mem") << "location " << x.value << " is " << result.value;

  // populate types
  copy(++x.types.begin(), x.types.end(), inserter(result.types, result.types.begin()));

  // drop-one 'deref'
  int i = 0;
  int len = x.properties.size();
  while (true) {
    assert(i < len);
    if (x.properties[i].first == "deref") break;
    result.properties.push_back(x.properties[i]);
    ++i;
  }
  ++i;  // skip first deref
  while (i < len) {
    result.properties.push_back(x.properties[i]);
  }
  return result;
}

//: 'get' can read from record address
:(scenario "get_indirect")
recipe main [
  1:integer <- copy 2:literal
  2:integer <- copy 34:literal
  3:integer <- copy 35:literal
  4:integer <- get 1:address:point/deref, 0:offset
]
+run: instruction main/3
+run: address to copy is 2
+run: product 0 is 34
+mem: storing in location 4

:(replace{} "case GET:")
case GET: {
  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
  reagent base = canonize(instructions[pc].ingredients[0]);
  int base_address = base.value;
  int base_type = base.types[0];
  assert(Type[base_type].is_record);
  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
  assert(instructions[pc].ingredients[1].types.size() == 1);
  assert(instructions[pc].ingredients[1].types[0] == 0);  // must be literal
  size_t offset = instructions[pc].ingredients[1].value;
  int src = base_address;
  for (size_t i = 0; i < offset; ++i) {
    src += size_of(reagent(Type[base_type].elements[i][0]));
  }
  trace("run") << "address to copy is " << src;
  assert(Type[base_type].is_record);
  assert(Type[base_type].elements.size() > offset);
  int src_type = Type[base_type].elements[offset][0];
  trace("run") << "its type is " << src_type;
  reagent tmp(src_type);
  tmp.value = src;
  tmp.types.push_back(src_type);
  vector<int> result(read_memory(tmp));
  trace("run") << "product 0 is " << result[0];
  write_memory(instructions[pc].products[0], result);
  break;
}

:(scenario "get_address_indirect")
# 'get' can read from record address
recipe main [
  1:integer <- copy 2:literal
  2:integer <- copy 34:literal
  3:integer <- copy 35:literal
  4:integer <- get-address 1:address:point/deref, 0:offset
]
+run: instruction main/3
+run: address to copy is 2
+run: product 0 is 2

:(replace{} "case GET_ADDRESS:")
case GET_ADDRESS: {
  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
  reagent base = canonize(instructions[pc].ingredients[0]);
  int base_address = base.value;
  int base_type = base.types[0];
  assert(Type[base_type].is_record);
  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
  assert(instructions[pc].ingredients[1].types.size() == 1);
  assert(instructions[pc].ingredients[1].types[0] == 0);  // must be literal
  size_t offset = instructions[pc].ingredients[1].value;
  int src = base_address;
  for (size_t i = 0; i < offset; ++i) {
    src += size_of(reagent(Type[base_type].elements[i][0]));
  }
  trace("run") << "address to copy is " << src;
  vector<int> result;
  result.push_back(src);
  trace("run") << "product 0 is " << result[0];
  write_memory(instructions[pc].products[0], result);
  break;
}
17'>517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>

#include <locale.h>

#include "ui/keyhandlers.h"
#include "ui/inputwin.h"
#include "tests/helpers.h"

static char line[INP_WIN_MAX];

// append

void append_to_empty(void **state)
{
    setlocale(LC_ALL, "");
    line[0] = '\0';
    int line_utf8_pos = 0;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_printable(line, &line_utf8_pos, &col, &pad_start, 'a', 80);

    assert_string_equal("a", line);
    assert_int_equal(line_utf8_pos, 1);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void append_wide_to_empty(void **state)
{
    setlocale(LC_ALL, "");
    line[0] = '\0';
    int line_utf8_pos = 0;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_printable(line, &line_utf8_pos, &col, &pad_start, 0x56DB, 80);

    assert_string_equal("四", line);
    assert_int_equal(line_utf8_pos, 1);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void append_to_single(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "a", 1);
    line[1] = '\0';
    int line_utf8_pos = 1;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_printable(line, &line_utf8_pos, &col, &pad_start, 'b', 80);

    assert_string_equal("ab", line);
    assert_int_equal(line_utf8_pos, 2);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}


void append_wide_to_single_non_wide(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "a", 1);
    line[1] = '\0';
    int line_utf8_pos = 1;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_printable(line, &line_utf8_pos, &col, &pad_start, 0x56DB, 80);

    assert_string_equal("a四", line);
    assert_int_equal(line_utf8_pos, 2);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void append_non_wide_to_single_wide(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "四", 1);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 1;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_printable(line, &line_utf8_pos, &col, &pad_start, 'b', 80);

    assert_string_equal("四b", line);
    assert_int_equal(line_utf8_pos, 2);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void append_wide_to_single_wide(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "四", 1);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 1;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_printable(line, &line_utf8_pos, &col, &pad_start, 0x4E09, 80);

    assert_string_equal("四三", line);
    assert_int_equal(line_utf8_pos, 2);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void append_non_wide_when_overrun(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "0123456789四1234567", 18);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 18;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_printable(line, &line_utf8_pos, &col, &pad_start, 'z', 20);
    key_printable(line, &line_utf8_pos, &col, &pad_start, 'z', 20);
    key_printable(line, &line_utf8_pos, &col, &pad_start, 'z', 20);

    assert_string_equal("0123456789四1234567zzz", line);
    assert_int_equal(line_utf8_pos, 21);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 3);
}

void insert_non_wide_to_non_wide(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd", 4);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 2;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_printable(line, &line_utf8_pos, &col, &pad_start, '0', 80);

    assert_string_equal("ab0cd", line);
    assert_int_equal(line_utf8_pos, 3);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void insert_single_non_wide_when_pad_scrolled(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "AAAAAAAAAAAAAAA", 15);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 2;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 2;

    key_printable(line, &line_utf8_pos, &col, &pad_start, 'B', 12);

    assert_string_equal("AABAAAAAAAAAAAAA", line);
    assert_int_equal(line_utf8_pos, 3);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 2);
}

void insert_many_non_wide_when_pad_scrolled(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "AAAAAAAAAAAAAAA", 15);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 2;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 2;

    key_printable(line, &line_utf8_pos, &col, &pad_start, 'B', 12);
    key_printable(line, &line_utf8_pos, &col, &pad_start, 'C', 12);
    key_printable(line, &line_utf8_pos, &col, &pad_start, 'D', 12);

    assert_string_equal("AABCDAAAAAAAAAAAAA", line);
    assert_int_equal(line_utf8_pos, 5);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 2);
}

void insert_single_non_wide_last_column(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcdefghijklmno", 15);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 7;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 2;

    key_printable(line, &line_utf8_pos, &col, &pad_start, '1', 5);

    assert_string_equal("abcdefg1hijklmno", line);
    assert_int_equal(line_utf8_pos, 8);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 3);
}

void insert_many_non_wide_last_column(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcdefghijklmno", 15);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 7;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 2;

    key_printable(line, &line_utf8_pos, &col, &pad_start, '1', 5);
    key_printable(line, &line_utf8_pos, &col, &pad_start, '2', 5);

    assert_string_equal("abcdefg12hijklmno", line);
    assert_int_equal(line_utf8_pos, 9);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 4);
}

void ctrl_left_when_no_input(void **state)
{
    setlocale(LC_ALL, "");
    line[0] = '\0';
    int line_utf8_pos = 0;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 0);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_at_start(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 0;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 0);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_in_first_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 2;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 0);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_in_first_space(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 4;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 0);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_at_start_of_second_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 5;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 0);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_in_second_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 8;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 5);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_at_end_of_second_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 10;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 5);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_in_second_space(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 11;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 5);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_at_start_of_third_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 12;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 5);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_in_third_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 14;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 12);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_at_end_of_third_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 15;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 12);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_in_third_space(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 16;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 12);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_at_end(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "abcd efghij klmn opqr", 21);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 20;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 17);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_in_only_whitespace(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "       ", 7);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 5;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 0);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_start_whitespace_start_of_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "    hello", 9);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 4;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 0);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_start_whitespace_middle_of_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "    hello", 9);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 7;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 4);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_in_whitespace_between_words(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "hey    hello", 12);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 5;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 0);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_in_whitespace_between_words_start_of_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "hey    hello", 12);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 7;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 0);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_in_whitespace_between_words_middle_of_word(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "hey    hello", 12);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 9;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 7);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_left_when_word_overrun_to_left(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "someword anotherword", 20);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 18;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 14;

    key_ctrl_left(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 9);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 9);
}

void ctrl_right_when_no_input(void **state)
{
    setlocale(LC_ALL, "");
    line[0] = '\0';
    int line_utf8_pos = 0;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 0);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_when_at_end(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "someword anotherword", 20);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 20;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 20);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_one_word_at_start(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "someword", 8);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 0;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 8);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_one_word_in_middle(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "someword", 8);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 3;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 8);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_one_word_at_end(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "someword", 8);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 7;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 8);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_two_words_from_middle_first(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "someword anotherword", 20);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 4;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 8);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_two_words_from_end_first(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "someword anotherword", 20);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 7;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 8);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_two_words_from_space(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "someword anotherword", 20);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 8;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 20);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_two_words_from_start_second(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "someword anotherword", 20);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 9;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 20);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_one_word_leading_whitespace(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "       someword", 15);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 3;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 15);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_two_words_in_whitespace(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "       someword        adfasdf", 30);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 19;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 30);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}

void ctrl_right_trailing_whitespace_from_middle(void **state)
{
    setlocale(LC_ALL, "");
    g_utf8_strncpy(line, "someword        ", 16);
    line[strlen(line)] = '\0';
    int line_utf8_pos = 3;
    int col = utf8_pos_to_col(line, line_utf8_pos);
    int pad_start = 0;

    key_ctrl_right(line, &line_utf8_pos, &col, &pad_start, 80);

    assert_int_equal(line_utf8_pos, 8);
    assert_int_equal(col, utf8_pos_to_col(line, line_utf8_pos));
    assert_int_equal(pad_start, 0);
}