Skip to content
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

Fix some failing test cases from toml-test #87

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
toml_cat
toml_json
toml_sample
/unittest/t1
/toml-test/toml-test

# Debug files
*.dSYM/
*.su
*.su
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ clean:
format:
clang-format -i $(shell find . -name '*.[ch]')

.PHONY: all clean install format
toml-test: all
@./toml-test/build.sh
@./toml-test/run.sh

check: toml-test
@make -C unittest
@./unittest/t1
./stdex/RUN.sh

.PHONY: all clean install format toml-test check
24 changes: 5 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ TOML in c99; v1.0 compliant.
If you are looking for a C++ library, you might try this wrapper: [https://github.com/cktan/tomlcpp](https://github.com/cktan/tomlcpp).

* Compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0).
* Tested with multiple test suites, including
[toml-lang/toml-test](https://github.com/toml-lang/toml-test) and
[iarna/toml-spec-tests](https://github.com/iarna/toml-spec-tests).
* Tested with [toml-lang/toml-test](https://github.com/toml-lang/toml-test).
* Provides very simple and intuitive interface.


Expand Down Expand Up @@ -174,21 +172,9 @@ Alternatively, specify `make install prefix=/a/file/path` to install into
## Testing
To test against the standard test set provided by toml-lang/toml-test:
Run `make toml-test` to test against the standard test set provided by
toml-lang/toml-test.
```sh
% make
% cd test1
% bash build.sh # do this once
% bash run.sh # this will run the test suite
```


To test against the standard test set provided by iarna/toml:
You will need to have Go installed for this to work.
```sh
% make
% cd test2
% bash build.sh # do this once
% bash run.sh # this will run the test suite
```
Run `make check` to run the toml-test tests and some additional unit tests.
22 changes: 17 additions & 5 deletions stdex/RUN.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
#!/usr/bin/env bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd "$DIR"
rm -f *.out

ok=0
fail=0
for i in *.toml; do
echo -n $i
../toml_cat $i >& $i.out
if [ -f $i.res ]; then
if $(diff $i.out $i.res >& /dev/null); then
echo " [OK]"
ok=$(( ok + 1 ))
else
echo " [FAILED]"
fail=$(( fail + 1 ))
echo >&2 "$0: $i [FAILED]"
diff -u $i.out $i.res >&2
echo >&2
fi
else
echo " [?????]"
echo "$i [?????]"
fail=$(( fail + 1 ))
fi

done

echo "$0: ok: $ok fail: $fail"
2 changes: 1 addition & 1 deletion stdex/float6.toml.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
sf3 = -inf,
sf4 = nan,
sf5 = nan,
sf6 = nan,
sf6 = -nan,
}
3 changes: 0 additions & 3 deletions test1/.gitignore

This file was deleted.

5 changes: 0 additions & 5 deletions test1/run.sh

This file was deleted.

1 change: 0 additions & 1 deletion test2/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions test2/Note.txt

This file was deleted.

7 changes: 0 additions & 7 deletions test2/build.sh

This file was deleted.

44 changes: 0 additions & 44 deletions test2/run.sh

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
44 changes: 44 additions & 0 deletions toml-test/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

failing=(
-skip valid/key/escapes # Treats "\n" as invalid because it first replaces escapes and then checks.
-skip valid/key/quoted-unicode # Doesn't print null byte correctly.
-skip valid/string/quoted-unicode
-skip valid/spec/string-7 # Lots of ''''''' ... that somehow goes wrong.
-skip invalid/string/literal-multiline-quotes-1
-skip invalid/string/literal-multiline-quotes-2
-skip invalid/string/multiline-quotes-1
-skip invalid/inline-table/trailing-comma # Trailing comma should be error; not worth fixing as it'll be allowed in 1.1
-skip invalid/inline-table/add # Appending existing tables
-skip invalid/array/extending-table
-skip invalid/table/append-with-dotted-keys-1
-skip invalid/table/append-with-dotted-keys-2
-skip invalid/control/bare-cr # Doesn't reject some forbidden control characters.
-skip invalid/control/bare-null
-skip invalid/control/comment-cr
-skip invalid/control/comment-del
-skip invalid/control/comment-lf
-skip invalid/control/comment-null
-skip invalid/control/comment-us
-skip invalid/encoding/bad-codepoint # Doesn't reject invalid UTF-8; nothing is multi-byte aware, so...
-skip invalid/encoding/bad-utf8-in-comment
-skip invalid/encoding/bad-utf8-in-multiline-literal
-skip invalid/encoding/bad-utf8-in-multiline
-skip invalid/encoding/bad-utf8-in-string-literal
-skip invalid/encoding/bad-utf8-in-string
-skip invalid/encoding/utf16

# These seem broken in toml-test...
-skip valid/spec/local-date-time-0
-skip valid/spec/local-time-0
-skip valid/spec/offset-date-time-0

# Fixed in master:
# https://github.com/toml-lang/toml-test/commit/fe8e1e2fb8b0309d54d6ad677aecb86bcb0ff138
-skip valid/float/inf-and-nan
-skip valid/spec/float-2
)

$DIR/toml-test $DIR/../toml_json ${failing[@]} "$@"
46 changes: 38 additions & 8 deletions toml.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Expand Down Expand Up @@ -198,13 +199,12 @@ int toml_utf8_to_ucs(const char *orig, int len, int64_t *ret) {
int toml_ucs_to_utf8(int64_t code, char buf[6]) {
/* http://stackoverflow.com/questions/6240055/manually-converting-unicode-codepoints-into-utf-8-and-utf-16
*/
/* The UCS code values 0xd800–0xdfff (UTF-16 surrogates) as well
* as 0xfffe and 0xffff (UCS noncharacters) should not appear in
/* The UCS code values 0xd800–0xdfff (UTF-16 surrogates) should not appear in
* conforming UTF-8 streams.
*/
if (0xd800 <= code && code <= 0xdfff)
return -1;
if (0xfffe <= code && code <= 0xffff)
if (0x10FFFF < code)
return -1;

/* 0x00000000 - 0x0000007F:
Expand Down Expand Up @@ -566,7 +566,7 @@ static char *norm_basic_str(const char *src, int srclen, int multiline,
xfree(dst);
return 0;
}
ch = *sp++;
ch = toupper(*sp++);
int v = ('0' <= ch && ch <= '9')
? ch - '0'
: (('A' <= ch && ch <= 'F') ? ch - 'A' + 10 : -1);
Expand Down Expand Up @@ -1694,7 +1694,7 @@ static int scan_string(context_t *ctx, char *p, int lineno, int dotisspecial) {
}
if (hexreq) {
hexreq--;
if (strchr("0123456789ABCDEF", *p))
if (strchr("0123456789ABCDEFabcdef", *p))
continue;
return e_syntax(ctx, lineno, "expect hex char");
}
Expand Down Expand Up @@ -1743,7 +1743,7 @@ static int scan_string(context_t *ctx, char *p, int lineno, int dotisspecial) {
}
if (hexreq) {
hexreq--;
if (strchr("0123456789ABCDEF", *p))
if (strchr("0123456789ABCDEFabcdef", *p))
continue;
return e_syntax(ctx, lineno, "expect hex char");
}
Expand Down Expand Up @@ -1982,6 +1982,8 @@ int toml_rtots(toml_raw_t src_, toml_timestamp_t *ret) {

/* parse date YYYY-MM-DD */
if (0 == scan_date(p, year, month, day)) {
if (*month < 1 || *day < 1 || *month > 12 || *day > 31)
return -1;
ret->year = year;
ret->month = month;
ret->day = day;
Expand All @@ -1998,6 +2000,8 @@ int toml_rtots(toml_raw_t src_, toml_timestamp_t *ret) {

/* parse time HH:MM:SS */
if (0 == scan_time(p, hour, minute, second)) {
if (*second < 0 || *minute < 0 || *hour < 0 || *hour > 23 || *minute > 59 || *second > 60)
return -1;
ret->hour = hour;
ret->minute = minute;
ret->second = second;
Expand Down Expand Up @@ -2081,10 +2085,13 @@ int toml_rtoi(toml_raw_t src, int64_t *ret_) {
int base = 0;
int64_t dummy;
int64_t *ret = ret_ ? ret_ : &dummy;
bool have_sign = false;

/* allow +/- */
if (s[0] == '+' || s[0] == '-')
if (s[0] == '+' || s[0] == '-') {
have_sign = true;
*p++ = *s++;
}

/* disallow +_100 */
if (s[0] == '_')
Expand Down Expand Up @@ -2112,6 +2119,14 @@ int toml_rtoi(toml_raw_t src, int64_t *ret_) {
if (s[1])
return -1;
}
if (!*s)
return -1;
// disallow +0xff, -0xff
if (have_sign)
return -1;
// disallow 0x_, 0o_, 0b_
if (s[0] == '_')
return -1;
}

/* just strip underscores and pass to strtoll */
Expand Down Expand Up @@ -2152,6 +2167,7 @@ int toml_rtod_ex(toml_raw_t src, double *ret_, char *buf, int buflen) {
const char *s = src;
double dummy;
double *ret = ret_ ? ret_ : &dummy;
bool have_us = false;

/* allow +/- */
if (s[0] == '+' || s[0] == '-')
Expand Down Expand Up @@ -2179,14 +2195,24 @@ int toml_rtod_ex(toml_raw_t src, double *ret_, char *buf, int buflen) {
while (*s && p < q) {
int ch = *s++;
if (ch == '_') {
have_us = true;
// disallow '__'
if (s[0] == '_')
return -1;
// disallow _e
if (s[0] == 'e')
return -1;
// disallow last char '_'
if (s[0] == 0)
return -1;
continue; /* skip _ */
}
// inf and nan are case-sensitive.
if (ch == 'I' || ch == 'N' || ch == 'F' || ch == 'A')
return -1;
// disallow e_
if (ch == 'e' && s[0] == '_')
return -1;
*p++ = ch;
}
if (*s || p == q)
Expand All @@ -2199,7 +2225,11 @@ int toml_rtod_ex(toml_raw_t src, double *ret_, char *buf, int buflen) {
char *endp;
errno = 0;
*ret = strtod(buf, &endp);
return (errno || *endp) ? -1 : 0;
if (errno || *endp)
return -1;
if (have_us && (isnan(*ret) || isinf(*ret)))
return -1;
return 0;
}

int toml_rtod(toml_raw_t src, double *ret_) {
Expand Down
5 changes: 4 additions & 1 deletion toml_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ static void print_escape_string(const char *s) {
printf("\\\\");
break;
default:
printf("%c", ch);
if (ch >= 0x00 && ch <= 0x1f)
printf("\\u00%X", ch);
else
printf("%c", ch);
break;
}
}
Expand Down
Loading