Add string utility functions and unit tests.
This commit is contained in:
parent
b88e23a7d2
commit
464c94501d
@ -28,15 +28,17 @@ noinst_LTLIBRARIES = libguac_common.la
|
|||||||
|
|
||||||
noinst_HEADERS = \
|
noinst_HEADERS = \
|
||||||
guac_io.h \
|
guac_io.h \
|
||||||
guac_dot_cursor.c \
|
guac_dot_cursor.h \
|
||||||
guac_list.h \
|
guac_list.h \
|
||||||
guac_pointer_cursor.c
|
guac_pointer_cursor.h \
|
||||||
|
guac_string.h
|
||||||
|
|
||||||
libguac_common_la_SOURCES = \
|
libguac_common_la_SOURCES = \
|
||||||
guac_io.c \
|
guac_io.c \
|
||||||
guac_dot_cursor.c \
|
guac_dot_cursor.c \
|
||||||
guac_list.c \
|
guac_list.c \
|
||||||
guac_pointer_cursor.c
|
guac_pointer_cursor.c \
|
||||||
|
guac_string.c
|
||||||
|
|
||||||
libguac_common_la_LIBADD = @LIBGUAC_LTLIB@
|
libguac_common_la_LIBADD = @LIBGUAC_LTLIB@
|
||||||
|
|
||||||
|
93
src/common/guac_string.c
Normal file
93
src/common/guac_string.c
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Glyptodon LLC
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include "guac_string.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int guac_count_occurrences(const char* string, char c) {
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
while (*string != 0) {
|
||||||
|
|
||||||
|
/* Count each occurrence */
|
||||||
|
if (*string == c)
|
||||||
|
count++;
|
||||||
|
|
||||||
|
/* Next character */
|
||||||
|
string++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
char** guac_split(const char* string, char delim) {
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
int token_count = guac_count_occurrences(string, delim) + 1;
|
||||||
|
const char* token_start = string;
|
||||||
|
|
||||||
|
/* Allocate space for tokens */
|
||||||
|
char** tokens = malloc(sizeof(char*) * (token_count+1));
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
int length;
|
||||||
|
char* token;
|
||||||
|
|
||||||
|
/* Find end of token */
|
||||||
|
while (*string != 0 && *string != delim)
|
||||||
|
string++;
|
||||||
|
|
||||||
|
/* Calculate token length */
|
||||||
|
length = string - token_start;
|
||||||
|
|
||||||
|
/* Allocate space for token and NULL terminator */
|
||||||
|
tokens[i++] = token = malloc(length + 1);
|
||||||
|
|
||||||
|
/* Copy token, store null */
|
||||||
|
memcpy(token, token_start, length);
|
||||||
|
token[length] = 0;
|
||||||
|
|
||||||
|
/* Stop at end of string */
|
||||||
|
if (*string == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Next token */
|
||||||
|
token_start = ++string;
|
||||||
|
|
||||||
|
} while (i < token_count);
|
||||||
|
|
||||||
|
/* NULL terminator */
|
||||||
|
tokens[i] = NULL;
|
||||||
|
|
||||||
|
return tokens;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
50
src/common/guac_string.h
Normal file
50
src/common/guac_string.h
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Glyptodon LLC
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __GUAC_COMMON_STRING_H
|
||||||
|
#define __GUAC_COMMON_STRING_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Counts the number of occurrences of a given character in a string.
|
||||||
|
*
|
||||||
|
* @param string The string to count occurrences within.
|
||||||
|
* @param c The character to count occurrences of.
|
||||||
|
* @return The number of occurrences.
|
||||||
|
*/
|
||||||
|
int guac_count_occurrences(const char* string, char c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Splits a string into a newly-allocated array of strings. The array itself
|
||||||
|
* and each string within the array will eventually need to be freed. The array
|
||||||
|
* is NULL-terminated.
|
||||||
|
*
|
||||||
|
* @param string The string to split.
|
||||||
|
* @param delim The character which separates individual substrings within the
|
||||||
|
* given string.
|
||||||
|
* @return A newly-allocated, NULL-terminated array of strings.
|
||||||
|
*/
|
||||||
|
char** guac_split(const char* string, char delim);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -22,13 +22,14 @@
|
|||||||
|
|
||||||
AUTOMAKE_OPTIONS = foreign
|
AUTOMAKE_OPTIONS = foreign
|
||||||
ACLOCAL_AMFLAGS = -I m4
|
ACLOCAL_AMFLAGS = -I m4
|
||||||
AM_CFLAGS = -Werror -Wall -pedantic @LIBGUAC_INCLUDE@
|
AM_CFLAGS = -Werror -Wall -pedantic @LIBGUAC_INCLUDE@ @COMMON_INCLUDE@
|
||||||
|
|
||||||
TESTS = test_libguac
|
TESTS = test_libguac
|
||||||
check_PROGRAMS = test_libguac
|
check_PROGRAMS = test_libguac
|
||||||
|
|
||||||
noinst_HEADERS = \
|
noinst_HEADERS = \
|
||||||
client/client_suite.h \
|
client/client_suite.h \
|
||||||
|
common/common_suite.h \
|
||||||
protocol/suite.h \
|
protocol/suite.h \
|
||||||
util/util_suite.h
|
util/util_suite.h
|
||||||
|
|
||||||
@ -37,6 +38,8 @@ test_libguac_SOURCES = \
|
|||||||
client/client_suite.c \
|
client/client_suite.c \
|
||||||
client/buffer_pool.c \
|
client/buffer_pool.c \
|
||||||
client/layer_pool.c \
|
client/layer_pool.c \
|
||||||
|
common/common_suite.c \
|
||||||
|
common/guac_string.c \
|
||||||
protocol/suite.c \
|
protocol/suite.c \
|
||||||
protocol/base64_decode.c \
|
protocol/base64_decode.c \
|
||||||
protocol/instruction_parse.c \
|
protocol/instruction_parse.c \
|
||||||
@ -47,5 +50,5 @@ test_libguac_SOURCES = \
|
|||||||
util/guac_pool.c \
|
util/guac_pool.c \
|
||||||
util/guac_unicode.c
|
util/guac_unicode.c
|
||||||
|
|
||||||
test_libguac_LDADD = @LIBGUAC_LTLIB@ @CUNIT_LIBS@
|
test_libguac_LDADD = @LIBGUAC_LTLIB@ @CUNIT_LIBS@ @COMMON_LTLIB@
|
||||||
|
|
||||||
|
56
tests/common/common_suite.c
Normal file
56
tests/common/common_suite.c
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Glyptodon LLC
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include "common_suite.h"
|
||||||
|
|
||||||
|
#include <CUnit/Basic.h>
|
||||||
|
|
||||||
|
int common_suite_init() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int common_suite_cleanup() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int register_common_suite() {
|
||||||
|
|
||||||
|
/* Add common test suite */
|
||||||
|
CU_pSuite suite = CU_add_suite("common",
|
||||||
|
common_suite_init, common_suite_cleanup);
|
||||||
|
if (suite == NULL) {
|
||||||
|
CU_cleanup_registry();
|
||||||
|
return CU_get_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Add tests */
|
||||||
|
if (CU_add_test(suite, "guac-string", test_guac_string) == NULL) {
|
||||||
|
CU_cleanup_registry();
|
||||||
|
return CU_get_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
48
tests/common/common_suite.h
Normal file
48
tests/common/common_suite.h
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Glyptodon LLC
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _GUAC_TEST_COMMON_SUITE_H
|
||||||
|
#define _GUAC_TEST_COMMON_SUITE_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test suite containing unit tests for the "common" utility library included
|
||||||
|
* for the sake of simplifying guacamole-server development, but not included
|
||||||
|
* as part of libguac.
|
||||||
|
*
|
||||||
|
* @file common_suite.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers the common test suite with CUnit.
|
||||||
|
*/
|
||||||
|
int register_common_suite();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for string utility functions.
|
||||||
|
*/
|
||||||
|
void test_guac_string();
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
73
tests/common/guac_string.c
Normal file
73
tests/common/guac_string.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Glyptodon LLC
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include "common_suite.h"
|
||||||
|
#include "guac_string.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <CUnit/Basic.h>
|
||||||
|
|
||||||
|
void test_guac_string() {
|
||||||
|
|
||||||
|
char** tokens;
|
||||||
|
|
||||||
|
/* Test occurrence counting */
|
||||||
|
CU_ASSERT_EQUAL(4, guac_count_occurrences("this is a test string", 's'));
|
||||||
|
CU_ASSERT_EQUAL(3, guac_count_occurrences("this is a test string", 'i'));
|
||||||
|
CU_ASSERT_EQUAL(0, guac_count_occurrences("", 's'));
|
||||||
|
|
||||||
|
/* Split test string */
|
||||||
|
tokens = guac_split("this is a test string", ' ');
|
||||||
|
|
||||||
|
CU_ASSERT_PTR_NOT_NULL(tokens);
|
||||||
|
|
||||||
|
/* Check resulting tokens */
|
||||||
|
CU_ASSERT_PTR_NOT_NULL_FATAL(tokens[0]);
|
||||||
|
CU_ASSERT_STRING_EQUAL("this", tokens[0]);
|
||||||
|
|
||||||
|
CU_ASSERT_PTR_NOT_NULL_FATAL(tokens[1]);
|
||||||
|
CU_ASSERT_STRING_EQUAL("is", tokens[1]);
|
||||||
|
|
||||||
|
CU_ASSERT_PTR_NOT_NULL_FATAL(tokens[2]);
|
||||||
|
CU_ASSERT_STRING_EQUAL("a", tokens[2]);
|
||||||
|
|
||||||
|
CU_ASSERT_PTR_NOT_NULL_FATAL(tokens[3]);
|
||||||
|
CU_ASSERT_STRING_EQUAL("test", tokens[3]);
|
||||||
|
|
||||||
|
CU_ASSERT_PTR_NOT_NULL_FATAL(tokens[4]);
|
||||||
|
CU_ASSERT_STRING_EQUAL("string", tokens[4]);
|
||||||
|
|
||||||
|
CU_ASSERT_PTR_NULL(tokens[5]);
|
||||||
|
|
||||||
|
|
||||||
|
/* Clean up */
|
||||||
|
free(tokens[0]);
|
||||||
|
free(tokens[1]);
|
||||||
|
free(tokens[2]);
|
||||||
|
free(tokens[3]);
|
||||||
|
free(tokens[4]);
|
||||||
|
free(tokens);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -23,6 +23,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include "client/client_suite.h"
|
#include "client/client_suite.h"
|
||||||
|
#include "common/common_suite.h"
|
||||||
#include "protocol/suite.h"
|
#include "protocol/suite.h"
|
||||||
#include "util/util_suite.h"
|
#include "util/util_suite.h"
|
||||||
|
|
||||||
@ -38,6 +39,7 @@ int main() {
|
|||||||
register_protocol_suite();
|
register_protocol_suite();
|
||||||
register_client_suite();
|
register_client_suite();
|
||||||
register_util_suite();
|
register_util_suite();
|
||||||
|
register_common_suite();
|
||||||
|
|
||||||
/* Run tests */
|
/* Run tests */
|
||||||
CU_basic_set_mode(CU_BRM_VERBOSE);
|
CU_basic_set_mode(CU_BRM_VERBOSE);
|
||||||
|
Loading…
Reference in New Issue
Block a user