Skip to content

Add token tests #4113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ master 8.16
- add vips_sdf(), vips_clamp(), vips_maxpair(), vips_minpair()
- more const for the C++ API [Julianiolo]
- deprecate "cache" (use tilecache instead)
- add tests for tokenisation

8.15.4

Expand Down
2 changes: 2 additions & 0 deletions libvips/include/vips/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ typedef enum {
VIPS_TOKEN_COMMA
} VipsToken;

// we expose this one in the API for testing
VIPS_API
const char *vips__token_get(const char *buffer,
VipsToken *token, char *string, int size);
const char *vips__token_must(const char *buffer, VipsToken *token,
Expand Down
17 changes: 17 additions & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ foreach script_test : script_tests
)
endforeach

test_token = executable('test_token',
'test_token.c',
dependencies: libvips_dep,
)

test_token_sh = configure_file(
input: 'test_token.sh',
output: 'test_token.sh',
copy: true,
)

test('token',
test_token_sh,
depends: test_token,
workdir: meson.current_build_dir(),
)

test_connections = executable('test_connections',
'test_connections.c',
dependencies: libvips_dep,
Expand Down
59 changes: 59 additions & 0 deletions test/test_token.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* compile with
*
* gcc -g -Wall token.c `pkg-config vips --cflags --libs`
*
* run with eg.:
*
* $ ./a.out '"wdfw"df,wdw,dw' 3 wdfw df,wdw,dw
*
*/

#include <vips/vips.h>

char *token_names[] = {
"",
"left",
"right",
"string",
"equals",
"comma",
};

int
main(int argc, char **argv)
{
if (VIPS_INIT(argv[0]))
vips_error_exit(NULL);

if (argc != 5)
vips_error_exit("usage: %s string-to-parse token token-string residual",
argv[0]);

const char *p = argv[1];
printf("argv[1]:\n");
for (int i = 0; i < strlen(p); i++)
printf("\t\t%2d) %02x %c\n", i, p[i], p[i]);

VipsToken token;
char buf[256];
p = vips__token_get(p, &token, buf, 256);

printf("vips__token_get:\n");
printf("\ttoken = %d (%s)\n", token, token_names[token]);
if (token == VIPS_TOKEN_STRING) {
printf("\tbuf = <%s>\n", buf);
for (int i = 0; i < strlen(buf); i++)
printf("\t\t%2d) 0x%02x %c\n", i, buf[i], buf[i]);
}
printf("\tresidual = <%s>\n", p);

if (token != atoi(argv[2]))
vips_error_exit("token mismatch");
if (token == VIPS_TOKEN_STRING &&
!g_str_equal(buf, argv[3]))
vips_error_exit("parsed string mismatch");
if (!g_str_equal(p, argv[4]))
vips_error_exit("residual mismatch");

return 0;
}
52 changes: 52 additions & 0 deletions test/test_token.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/sh

# test tokenisation

# set -x
# set -e

# args are:
# string-to-parse
# expected-token
# expected-string (for string tokens)
# expected-residual
check() {
test_name="$1"
test_string="$2"
residual="$3"
token="$4"
token_string="$5"

echo -n "$test_name ..."
log=$(./test_token "$2" "$3" "$4" "$5")
if [ $? -ne 0 ]; then
echo " FAIL"
echo "./test_token '$2' '$3' '$4' '$5'"
echo $log
exit 1
else
echo " yes"
fi
}

check "quoted strings end on the closing quote" \
'"ab"cd,abc,ab' 3 'ab' 'cd,abc,ab'

check "quoted strings can have escaped quotes" \
'"ab\"cd",abc,ab' 3 'ab"cd' ',abc,ab'

check "no closing quote" \
'" abcd ,abc,ab' 3 ' abcd ,abc,ab' ''

check "empty quote" \
'""abcd,abc,ab' 3 '' 'abcd,abc,ab'

check "skip whitespace around quoted strings" \
' " abcd " ,abc,ab' 3 ' abcd ' ' ,abc,ab'

check "unquoted strings can have embedded quotes" \
'ab"cd,abc,ab' 3 'ab"cd' ',abc,ab'

check "skip whitespace around unquoted strings" \
' abcd ,abc,ab' 3 'abcd' ',abc,ab'

Loading