https://github.com/akkartik/mu/blob/main/linux/crenshaw2-1b.subx
  1 # Port of https://github.com/akkartik/crenshaw/blob/master/tutor2.1.pas
  2 # which corresponds to the section "single digits" in https://compilers.iecc.com/crenshaw/tutor2.txt
  3 # except that we support hex numbers of multiple digits.
  4 #
  5 # To run:
  6 #   $ bootstrap/bootstrap translate [01]*.subx crenshaw2-1b.subx -o crenshaw2-1b
  7 #   $ echo '1a'  |bootstrap/bootstrap run crenshaw2-1b
  8 # Expected output:
  9 #   # syscall(exit, 1a)
 10 #   bb/copy-to-ebx  3/imm32
 11 #   b8/copy-to-eax  1/imm32/exit
 12 #   cd/syscall  0x80/imm8
 13 #
 14 # To run the generated output:
 15 #   $ echo '1a'  |bootstrap/bootstrap run crenshaw2-1b > z1.subx
 16 #   $ bootstrap/bootstrap translate z1.subx -o z1
 17 #   $ bootstrap/bootstrap run z1
 18 #   $ echo $?
 19 #   26  # 0x1a in decimal
 20 #
 21 # Stdin must contain just a single hex digit. Other input will print an error:
 22 #   $ echo 'xyz'  |bootstrap/bootstrap run crenshaw2-1b
 23 #   Error: integer expected
 24 #
 25 # Names in this file sometimes follow Crenshaw's original rather than my usual
 26 # naming conventions.
 27 
 28 == code
 29 #   instruction                     effective address                                                   register    displacement    immediate
 30 # . op          subop               mod             rm32          base        index         scale       r32
 31 # . 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
 32 
 33 Entry:  # run tests if necessary, call 'compile' if not
 34     # . prologue
 35     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 36 
 37     # initialize heap
 38     # . Heap = new-segment(Heap-size)
 39     # . . push args
 40     68/push  Heap/imm32
 41     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Heap-size/disp32                  # push *Heap-size
 42     # . . call
 43     e8/call  new-segment/disp32
 44     # . . discard args
 45     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 46 
 47     # - if argc > 1 and argv[1] == "test", then return run_tests()
 48     # if (argc <= 1) goto run-main
 49     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0/disp8         1/imm32           # compare *ebp
 50     7e/jump-if-<=  $run-main/disp8
 51     # if (!kernel-string-equal?(argv[1], "test")) goto run-main
 52     # . eax = kernel-string-equal?(argv[1], "test")
 53     # . . push args
 54     68/push  "test"/imm32
 55     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
 56     # . . call
 57     e8/call  kernel-string-equal?/disp32
 58     # . . discard args
 59     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 60     # . if (eax == false) goto run-main
 61     3d/compare-eax-and  0/imm32/false
 62     74/jump-if-=  $run-main/disp8
 63     # run-tests()
 64     e8/call  run-tests/disp32
 65     # syscall(exit, *Num-test-failures)
 66     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   Num-test-failures/disp32          # copy *Num-test-failures to ebx
 67     eb/jump  $main:end/disp8
 68 $run-main:
 69     # - otherwise read a program from stdin and emit its translation to stdout
 70     # . compile(Stdin, 1/stdout, 2/stderr, 0)
 71     # . . push args
 72     68/push  0/imm32/exit-descriptor
 73     68/push  2/imm32/stderr
 74     68/push  1/imm32/stdout
 75     68/push  Stdin/imm32
 76     # . . call
 77     e8/call  compile/disp32
 78     # . . discard args
 79     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
 80     # syscall(exit, 0)
 81     bb/copy-to-ebx  0/imm32
 82 $main:end:
 83     e8/call  syscall_exit/disp32
 84 
 85 # the main entry point
 86 compile:  # in: (addr buffered-file), out: fd or (addr stream byte), err: fd or (addr stream byte), ed: (addr exit-descriptor)
 87     # . prologue
 88     55/push-ebp
 89     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 90     # . save registers
 91     50/push-eax
 92     51/push-ecx
 93     # prime the pump
 94     # . Look = get-char(in)
 95     # . . push args
 96     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8      .                    # push *(ebp+8)
 97     # . . call
 98     e8/call  get-char/disp32
 99     # . . discard args
100     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
101     # var num/ecx: (stream byte 7)
102     # Numbers can be 32 bits or 8 hex bytes long. One of them will be in 'Look', so we need space for 7 bytes.
103     # Sizing the stream just right buys us overflow-handling for free inside 'get-num'.
104     # Add 12 bytes for 'read', 'write' and 'size' fields, for a total of 19 bytes, or 0x13 in hex.
105     # The stack pointer is no longer aligned, so dump_stack() can be misleading past this point.
106     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0x13/imm32        # subtract from esp
107     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
108     # initialize the stream
109     # . num->size = 7
110     c7          0/subop/copy        1/mod/*+disp8   1/rm32/ecx    .           .             .           .           8/disp8         7/imm32           # copy to *(ecx+8)
111     # . clear-stream(num)
112     # . . push args
113     51/push-ecx
114     # . . call
115     e8/call  clear-stream/disp32
116     # . . discard args
117     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
118     # read a digit from 'in' into 'num'
119     # . get-num(in, num, err, ed)
120     # . . push args
121     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
122     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
123     51/push-ecx/num
124     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8      .                    # push *(ebp+8)
125     # . . call
126     e8/call  get-num/disp32
127     # . . discard args
128     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
129     # render 'num' into the following template on 'out':
130     #   bb/copy-to-ebx  _num_
131     #   b8/copy-to-eax  1/imm32/exit
132     #   cd/syscall  0x80/imm8
133     #
134     # . write(out, "bb/copy-to-ebx  ")
135     # . . push args
136     68/push  "bb/copy-to-ebx  "/imm32
137     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
138     # . . call
139     e8/call  write/disp32
140     # . . discard args
141     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
142     # . write-stream(out, num)
143     # . . push args
144     51/push-ecx/num
145     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
146     # . . call
147     e8/call  write-stream/disp32
148     # . . discard args
149     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
150     # . write(out, Newline)
151     # . . push args
152     68/push  Newline/imm32
153     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
154     # . . call
155     e8/call  write/disp32
156     # . . discard args
157     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
158     # . write(out, "b8/copy-to-eax  1/imm32/exit\n")
159     # . . push args
160     68/push  "b8/copy-to-eax  1/imm32/exit\n"/imm32
161     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
162     # . . call
163     e8/call  write/disp32
164     # . . discard args
165     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
166     # . write(out, "cd/syscall  0x80/imm8\n")
167     # . . push args
168     68/push  "cd/syscall  0x80/imm8\n"/imm32
169     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
170     # . . call
171     e8/call  write/disp32
172     # . . discard args
173     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
174 $compile:end:
175     # . restore registers
176     59/pop-to-ecx
177     58/pop-to-eax
178     # . epilogue
179     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
180     5d/pop-to-ebp
181     c3/return
182 
183 # Read a sequence of digits into 'out'. Abort if there are none, or if there is
184 # no space in 'out'.
185 # Input comes from the global variable 'Look' (first byte) and the argument
186 # 'in' (rest). We leave the next byte from 'in' into 'Look' on exit.
187 get-num:  # in: (addr buffered-file), out: (addr stream byte), err: fd or (addr stream byte), ed: (addr exit-descriptor)
188     # pseudocode:
189     #   if (!digit?(Look)) expected(ed, err, "integer")
190     #   do
191     #     if out->write >= out->size
192     #       write(err, "Error: too many digits in number\n")
193     #       stop(ed, 1)
194     #     out->data[out->write] = LSB(Look)
195     #     ++out->write
196     #     Look = get-char(in)
197     #   while digit?(Look)
198     # This is complicated because I don't want to hard-code the error strategy in
199     # a general helper like write-byte-buffered. Maybe I should just create a
200     # local helper.
201     #
202     # within the loop we'll try to keep things in registers:
203     #   in: esi
204     #   out: edi
205     #   out->write: ecx (cached copy; need to keep in sync)
206     #   out->size: edx
207     #   temporaries: eax, ebx
208     # We can't allocate Look to a register because it gets written implicitly in
209     # get-char in each iteration of the loop. (Thereby demonstrating that it's
210     # not the right interface for us. But we'll keep it just to follow Crenshaw.)
211     #
212     # . prologue
213     55/push-ebp
214     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
215     # - if (digit?(Look)) expected(ed, err, "integer")
216     # . eax = digit?(Look)
217     # . . push args
218     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Look/disp32     .                 # push *Look
219     # . . call
220     e8/call  digit?/disp32
221     # . . discard args
222     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
223     # . if (eax == false)
224     3d/compare-eax-and  0/imm32/false
225     75/jump-if-!=  $get-num:main/disp8
226     # . expected(ed, err, "integer")
227     # . . push args
228     68/push  "integer"/imm32
229     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
230     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
231     # . . call
232     e8/call  expected/disp32  # never returns
233     # . . discard args
234     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
235 $get-num:main:
236     # - otherwise read a digit
237     # . save registers
238     50/push-eax
239     51/push-ecx
240     52/push-edx
241     53/push-ebx
242     56/push-esi
243     57/push-edi
244     # read necessary variables to registers
245     # esi = in
246     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
247     # edi = out
248     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to edi
249     # ecx = out->write
250     8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # copy *edi to ecx
251     # edx = out->size
252     8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(edi+8) to edx
253 $get-num:loop:
254     # if (out->write >= out->size) error
255     39/compare                      3/mod/direct    2/rm32/edx    .           .             .           1/r32/ecx   .               .                 # compare edx with ecx
256     7d/jump-if-<  $get-num:loop-stage2/disp8
257     # . error(ed, err, msg)  # TODO: show full number
258     # . . push args
259     68/push  "get-num: too many digits in number"/imm32
260     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
261     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
262     # . . call
263     e8/call  error/disp32  # never returns
264     # . . discard args
265     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
266 $get-num:loop-stage2:
267     # out->data[out->write] = LSB(Look)
268     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  1/index/ecx   .           3/r32/ebx   0xc/disp8       .                 # copy edi+ecx+12 to ebx
269     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Look/disp32     .                 # copy *Look to eax
270     88/copy-byte                    0/mod/indirect  3/rm32/ebx    .           .             .           0/r32/AL    .               .                 # copy byte at AL to *ebx
271     # ++out->write
272     41/increment-ecx
273     # Look = get-char(in)
274     # . . push args
275     56/push-esi
276     # . . call
277     e8/call  get-char/disp32
278     # . . discard args
279     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
280     # if (digit?(Look)) loop
281     # . eax = digit?(Look)
282     # . . push args
283     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Look/disp32     .                 # push *Look
284     # . . call
285     e8/call  digit?/disp32
286     # . . discard args
287     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
288     # . if (eax != false) loop
289     3d/compare-eax-and  0/imm32/false
290     0f 85/jump-if-!=  $get-num:loop/disp32
291 $get-num:loop-end:
292     # persist necessary variables from registers
293     89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # copy ecx to *edi
294 $get-num:end:
295     # . restore registers
296     5f/pop-to-edi
297     5e/pop-to-esi
298     5b/pop-to-ebx
299     5a/pop-to-edx
300     59/pop-to-ecx
301     58/pop-to-eax
302     # . epilogue
303     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
304     5d/pop-to-ebp
305     c3/return
306 
307 test-get-num-reads-single-digit:
308     # - check that get-num returns first character if it's a digit
309     # This test uses exit-descriptors. Use ebp for setting up local variables.
310     55/push-ebp
311     89/copy                         3/mod/direct    5/rm32/ebp    .           .           ="p">}

void
parse_cmd_three_args(void** state)
{
    char* inp = "/cmd arg1 arg2 arg3";
    gboolean result = FALSE;
    gchar** args = parse_args(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("arg3", args[2]);
    g_strfreev(args);
}

void
parse_cmd_three_args_with_spaces(void** state)
{
    char* inp = "  /cmd    arg1  arg2     arg3 ";
    gboolean result = FALSE;
    gchar** args = parse_args(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("arg3", args[2]);
    g_strfreev(args);
}

void
parse_cmd_with_freetext(void** state)
{
    char* inp = "/cmd this is some free text";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 1, 1, &result);

    assert_true(result);
    assert_int_equal(1, g_strv_length(args));
    assert_string_equal("this is some free text", args[0]);
    g_strfreev(args);
}

void
parse_cmd_one_arg_with_freetext(void** state)
{
    char* inp = "/cmd arg1 this is some free text";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 1, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("this is some free text", args[1]);
    g_strfreev(args);
}

void
parse_cmd_two_args_with_freetext(void** state)
{
    char* inp = "/cmd arg1 arg2 this is some free text";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 1, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("this is some free text", args[2]);
    g_strfreev(args);
}

void
parse_cmd_min_zero(void** state)
{
    char* inp = "/cmd";
    gboolean result = FALSE;
    gchar** args = parse_args(inp, 0, 2, &result);

    assert_true(result);
    assert_int_equal(0, g_strv_length(args));
    assert_null(args[0]);
    g_strfreev(args);
}

void
parse_cmd_min_zero_with_freetext(void** state)
{
    char* inp = "/cmd";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 0, 2, &result);

    assert_true(result);
    assert_int_equal(0, g_strv_length(args));
    assert_null(args[0]);
    g_strfreev(args);
}

void
parse_cmd_with_quoted(void** state)
{
    char* inp = "/cmd \"arg1\" arg2";
    gboolean result = FALSE;
    gchar** args = parse_args(inp, 2, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    g_strfreev(args);
}

void
parse_cmd_with_quoted_and_space(void** state)
{
    char* inp = "/cmd \"the arg1\" arg2";
    gboolean result = FALSE;
    gchar** args = parse_args(inp, 2, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("the arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    g_strfreev(args);
}

void
parse_cmd_with_quoted_and_many_spaces(void** state)
{
    char* inp = "/cmd \"the arg1 is here\" arg2";
    gboolean result = FALSE;
    gchar** args = parse_args(inp, 2, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("the arg1 is here", args[0]);
    assert_string_equal("arg2", args[1]);
    g_strfreev(args);
}

void
parse_cmd_with_many_quoted_and_many_spaces(void** state)
{
    char* inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\"";
    gboolean result = FALSE;
    gchar** args = parse_args(inp, 2, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("the arg1 is here", args[0]);
    assert_string_equal("and arg2 is right here", args[1]);
    g_strfreev(args);
}

void
parse_cmd_freetext_with_quoted(void** state)
{
    char* inp = "/cmd \"arg1\" arg2 hello there what's up";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("hello there what's up", args[2]);
    g_strfreev(args);
}

void
parse_cmd_freetext_with_quoted_and_space(void** state)
{
    char* inp = "/cmd \"the arg1\" arg2 another bit of freetext";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("the arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("another bit of freetext", args[2]);
    g_strfreev(args);
}

void
parse_cmd_freetext_with_quoted_and_many_spaces(void** state)
{
    char* inp = "/cmd \"the arg1 is here\" arg2 some more freetext";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("the arg1 is here", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("some more freetext", args[2]);
    g_strfreev(args);
}

void
parse_cmd_freetext_with_many_quoted_and_many_spaces(void** state)
{
    char* inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\" and heres the free text";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("the arg1 is here", args[0]);
    assert_string_equal("and arg2 is right here", args[1]);
    assert_string_equal("and heres the free text", args[2]);
    g_strfreev(args);
}

void
parse_cmd_with_quoted_freetext(void** state)
{
    char* inp = "/cmd arg1 here is \"some\" quoted freetext";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 1, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("here is \"some\" quoted freetext", args[1]);
    g_strfreev(args);
}

void
parse_cmd_with_third_arg_quoted_0_min_3_max(void** state)
{
    char* inp = "/group add friends \"The User\"";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 0, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("add", args[0]);
    assert_string_equal("friends", args[1]);
    assert_string_equal("The User", args[2]);

    g_strfreev(args);
}

void
parse_cmd_with_second_arg_quoted_0_min_3_max(void** state)
{
    char* inp = "/group add \"The Group\" friend";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 0, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("add", args[0]);
    assert_string_equal("The Group", args[1]);
    assert_string_equal("friend", args[2]);

    g_strfreev(args);
}

void
parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void** state)
{
    char* inp = "/group add \"The Group\" \"The User\"";
    gboolean result = FALSE;
    gchar** args = parse_args_with_freetext(inp, 0, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("add", args[0]);
    assert_string_equal("The Group", args[1]);
    assert_string_equal("The User", args[2]);

    g_strfreev(args);
}

void
count_one_token(void** state)
{
    char* inp = "one";
    int result = count_tokens(inp);

    assert_int_equal(1, result);
}

void
count_one_token_quoted_no_whitespace(void** state)
{
    char* inp = "\"one\"";
    int result = count_tokens(inp);

    assert_int_equal(1, result);
}

void
count_one_token_quoted_with_whitespace(void** state)
{
    char* inp = "\"one two\"";
    int result = count_tokens(inp);

    assert_int_equal(1, result);
}

void
count_two_tokens(void** state)
{
    char* inp = "one two";
    int result = count_tokens(inp);

    assert_int_equal(2, result);
}

void
count_two_tokens_first_quoted(void** state)
{
    char* inp = "\"one and\" two";
    int result = count_tokens(inp);

    assert_int_equal(2, result);
}

void
count_two_tokens_second_quoted(void** state)
{
    char* inp = "one \"two and\"";
    int result = count_tokens(inp);

    assert_int_equal(2, result);
}

void
count_two_tokens_both_quoted(void** state)
{
    char* inp = "\"one and then\" \"two and\"";
    int result = count_tokens(inp);

    assert_int_equal(2, result);
}

void
get_first_of_one(void** state)
{
    char* inp = "one";
    char* result = get_start(inp, 2);

    assert_string_equal("one", result);
    free(result);
}

void
get_first_of_two(void** state)
{
    char* inp = "one two";
    char* result = get_start(inp, 2);

    assert_string_equal("one ", result);
    free(result);
}

void
get_first_two_of_three(void** state)
{
    char* inp = "one two three";
    char* result = get_start(inp, 3);

    assert_string_equal("one two ", result);
    free(result);
}

void
get_first_two_of_three_first_quoted(void** state)
{
    char* inp = "\"one\" two three";
    char* result = get_start(inp, 3);

    assert_string_equal("\"one\" two ", result);
    free(result);
}

void
get_first_two_of_three_s