source: mainline/uspace/app/bdsh/test/toktest.c@ b5143bd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b5143bd was 134ac5d, checked in by Vojtech Horky <vojtechhorky@…>, 11 years ago

Update PCUT to newest version

  • Property mode set to 100644
File size: 2.7 KB
Line 
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
36PCUT_INIT
37
38#define MAX_TOKENS 32
39#define MAX_INPUT 256
40static tokenizer_t tokenizer;
41static token_t tokens[MAX_TOKENS];
42static char input_buffer[MAX_INPUT];
43
44/* Tokenize the input, asserts that number of tokens is okay. */
45static void prepare(const char *input, size_t expected_token_count) {
46 str_cpy(input_buffer, MAX_INPUT, input);
47
48 int rc = tok_init(&tokenizer, input_buffer, tokens, MAX_TOKENS);
49 PCUT_ASSERT_INT_EQUALS(EOK, rc);
50
51 size_t token_count;
52 rc = tok_tokenize(&tokenizer, &token_count);
53 PCUT_ASSERT_INT_EQUALS(EOK, rc);
54
55
56 PCUT_ASSERT_INT_EQUALS(expected_token_count, token_count);
57}
58
59
60#define ASSERT_TOKEN(index, token_type, token_text) \
61 do { \
62 PCUT_ASSERT_INT_EQUALS(token_type, tokens[index].type); \
63 PCUT_ASSERT_STR_EQUALS(token_text, tokens[index].text); \
64 } while (0)
65
66
67
68PCUT_TEST_SUITE(tokenizer);
69
70PCUT_TEST_AFTER {
71 /* Destroy the tokenizer. */
72 tok_fini(&tokenizer);
73}
74
75
76PCUT_TEST(empty_input) {
77 prepare("", 0);
78}
79
80PCUT_TEST(only_spaces) {
81 prepare(" ", 1);
82
83 ASSERT_TOKEN(0, TOKTYPE_SPACE, " ");
84}
85
86PCUT_TEST(two_text_tokens) {
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
95
96PCUT_MAIN()
Note: See TracBrowser for help on using the repository browser.