Skip to content

Commit

Permalink
Extract readline test to separate file, and insert it into check suite.
Browse files Browse the repository at this point in the history
  • Loading branch information
asterix24 committed Feb 6, 2012
1 parent 4490d77 commit 034aadd
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 58 deletions.
58 changes: 0 additions & 58 deletions bertos/mware/readline.c
Expand Up @@ -66,21 +66,16 @@
* \author Giovanni Bajo <rasky@develer.com>
*/


#include "readline.h"

#include <cfg/compiler.h>
#include <cfg/debug.h>

#include <stdio.h>

/// Enable compilation of the unit test code
#define DEBUG_UNIT_TEST 0

/// Enable dump of the history after each line
#define DEBUG_DUMP_HISTORY 0


/** Special keys (escape sequences converted to a single code) */
enum RL_KEYS {
SPECIAL_KEYS = 0x1000,
Expand Down Expand Up @@ -443,56 +438,3 @@ const char* rl_readline(struct RLContext* ctx)
return buf;
}


#if DEBUG_UNIT_TEST

/** Perform the unit test for the readline library */
void rl_test(void);

#if HISTORY_SIZE != 32
#error This test needs HISTORY_SIZE to be set at 32
#endif

static struct RLContext test_ctx;

static char* test_getc_ptr;
static int test_getc(void* data)
{
return *test_getc_ptr++;
}

/** Perform a readline test. The function pipes the characters from \a input_buffer
* through the I/O to \c rl_readline(). After the whole string is sent, \c do_test()
* checks if the current history within the context match \a expected_history.
*/
static bool do_test(char* input_buffer, char* expected_history)
{
rl_init_ctx(&test_ctx);
rl_sethook_get(&test_ctx, test_getc, NULL);

test_getc_ptr = input_buffer;
while (*test_getc_ptr)
rl_readline(&test_ctx);

if (memcmp(test_ctx.real_history, expected_history, HISTORY_SIZE) != 0)
{
ASSERT2(0, "history compare failed");
return false;
}

return true;
}

void rl_test(void)
{
char* test1_in = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\n";
char test1_hist[HISTORY_SIZE] = "\0l\0m\0n\0o\0p\0q\0r\0s\0t\0u\0v\0w\0x\0y\0z";

if (!do_test(test1_in, test1_hist))
return;

kprintf("rl_test successful\n");
}

#endif /* DEBUG_UNIT_TEST */

5 changes: 5 additions & 0 deletions bertos/mware/readline.h
Expand Up @@ -117,4 +117,9 @@ const char* rl_readline(struct RLContext* ctx);

void rl_refresh(struct RLContext* ctx);

/* Test prototype */
int readline_testSetup(void);
int readline_testRun(void);
int readline_testTearDown(void);

#endif /* MWARE_READLINE_H */
115 changes: 115 additions & 0 deletions bertos/mware/readline_test.c
@@ -0,0 +1,115 @@
/**
* \file
* <!--
* This file is part of BeRTOS.
*
* Bertos is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*
* Copyright 2012 Develer S.r.l. (http://www.develer.com/)
* Copyright 2004 Giovanni Bajo
* All Rights Reserved.
* -->
*
* \brief Readline module test.
*
* \author Giovanni Bajo <rasky@develer.com>
* \author Daniele Basile <asterix@develer.com>
*/

#include "readline.h"

#include <cfg/compiler.h>
#include <cfg/debug.h>
#include <cfg/test.h>

#include <stdio.h>


char* test1_in = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\n";
char test1_hist[HISTORY_SIZE] = "\0l\0m\0n\0o\0p\0q\0r\0s\0t\0u\0v\0w\0x\0y\0z";

/** Perform the unit test for the readline library */
void rl_test(void);

#if HISTORY_SIZE != 32
#error This test needs HISTORY_SIZE to be set at 32
#endif

static struct RLContext test_ctx;

static char* test_getc_ptr;
static int test_getc(void* data)
{
return *test_getc_ptr++;
}

/**
* Perform a readline test. The function pipes the characters from \a input_buffer
* through the I/O to \c rl_readline(). After the whole string is sent, \c do_test()
* checks if the current history within the context match \a expected_history.
*/
static bool do_test(char* input_buffer, char* expected_history)
{
rl_init_ctx(&test_ctx);
rl_sethook_get(&test_ctx, test_getc, NULL);

test_getc_ptr = input_buffer;
while (*test_getc_ptr)
rl_readline(&test_ctx);

if (memcmp(test_ctx.real_history, expected_history, HISTORY_SIZE) != 0)
{
ASSERT2(0, "history compare failed");
return false;
}

return true;
}


int readline_testSetup(void)
{
kdbg_init();

return 0;
}


int readline_testRun(void)
{
if (!do_test(test1_in, test1_hist))
return -1;

kprintf("rl_test successful\n");
return 0;
}

int readline_testTearDown(void)
{
return 0;
}

TEST_MAIN(readline);


1 change: 1 addition & 0 deletions test/run_tests.sh
Expand Up @@ -46,6 +46,7 @@ SRC_LIST="
bertos/mware/formatwr.c
bertos/mware/hex.c
bertos/mware/sprintf.c
bertos/mware/readline.c
bertos/os/hptime.c
bertos/struct/kfile_fifo.c
bertos/struct/heap.c
Expand Down

0 comments on commit 034aadd

Please sign in to comment.