1 | /*
|
---|
2 | * Copyright (c) 2014 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <errno.h>
|
---|
30 | #include <str.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 |
|
---|
33 | #include "../tok.h"
|
---|
34 | #include <pcut/pcut.h>
|
---|
35 |
|
---|
36 | PCUT_INIT;
|
---|
37 |
|
---|
38 | #define MAX_TOKENS 32
|
---|
39 | #define MAX_INPUT 256
|
---|
40 | static tokenizer_t tokenizer;
|
---|
41 | static token_t tokens[MAX_TOKENS];
|
---|
42 | static char input_buffer[MAX_INPUT];
|
---|
43 |
|
---|
44 | /* Tokenize the input, asserts that number of tokens is okay. */
|
---|
45 | static void prepare(const char *input, size_t expected_token_count)
|
---|
46 | {
|
---|
47 | str_cpy(input_buffer, MAX_INPUT, input);
|
---|
48 |
|
---|
49 | errno_t rc = tok_init(&tokenizer, input_buffer, tokens, MAX_TOKENS);
|
---|
50 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
51 |
|
---|
52 | size_t token_count;
|
---|
53 | rc = tok_tokenize(&tokenizer, &token_count);
|
---|
54 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
55 |
|
---|
56 | PCUT_ASSERT_INT_EQUALS(expected_token_count, token_count);
|
---|
57 | }
|
---|
58 |
|
---|
59 | #define ASSERT_TOKEN(index, token_type, token_text) \
|
---|
60 | do { \
|
---|
61 | PCUT_ASSERT_INT_EQUALS(token_type, tokens[index].type); \
|
---|
62 | PCUT_ASSERT_STR_EQUALS(token_text, tokens[index].text); \
|
---|
63 | } while (0)
|
---|
64 |
|
---|
65 | PCUT_TEST_SUITE(tokenizer);
|
---|
66 |
|
---|
67 | PCUT_TEST_AFTER
|
---|
68 | {
|
---|
69 | /* Destroy the tokenizer. */
|
---|
70 | tok_fini(&tokenizer);
|
---|
71 | }
|
---|
72 |
|
---|
73 | PCUT_TEST(empty_input)
|
---|
74 | {
|
---|
75 | prepare("", 0);
|
---|
76 | }
|
---|
77 |
|
---|
78 | PCUT_TEST(only_spaces)
|
---|
79 | {
|
---|
80 | prepare(" ", 1);
|
---|
81 |
|
---|
82 | ASSERT_TOKEN(0, TOKTYPE_SPACE, " ");
|
---|
83 | }
|
---|
84 |
|
---|
85 | PCUT_TEST(two_text_tokens)
|
---|
86 | {
|
---|
87 | prepare("alpha bravo", 3);
|
---|
88 |
|
---|
89 | ASSERT_TOKEN(0, TOKTYPE_TEXT, "alpha");
|
---|
90 | ASSERT_TOKEN(1, TOKTYPE_SPACE, " ");
|
---|
91 | ASSERT_TOKEN(2, TOKTYPE_TEXT, "bravo");
|
---|
92 | }
|
---|
93 |
|
---|
94 | PCUT_MAIN();
|
---|