Skip to content

Commit

Permalink
Added tests for UTF-8 decoding and improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Nov 25, 2017
1 parent 0c357a6 commit 189468b
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/b6b_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static enum b6b_res b6b_rand_proc_choice(struct b6b_interp *interp,
{
struct b6b_obj *l;
struct b6b_litem *li;
int i, c;
unsigned int i, c;

if (!b6b_proc_get_args(interp, args, "ol", NULL, &l))
return B6B_ERR;
Expand All @@ -35,14 +35,14 @@ static enum b6b_res b6b_rand_proc_choice(struct b6b_interp *interp,
if (!li)
return B6B_ERR;

c = rand_r(&interp->seed);
c = (unsigned int)(rand_r(&interp->seed) & UINT_MAX);

for (i = 0; i < c; ++i) {
for (i = 1; i < c; ++i) {
li = b6b_list_next(li);
if (!li) {
/* if the list length is smaller than the random number, divide the
* latter by the former */
c %= (i + 1);
c %= i;

li = b6b_list_first(l);
for (i = 1; i <= c; ++i)
Expand Down
11 changes: 10 additions & 1 deletion src/b6b_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,17 @@ struct b6b_obj *b6b_str_decode(const char *s, size_t len)
return NULL;
}

if (!out)
if (!out) {
/* if no characters were converted and this isn't the end of the
* string, there is a \0 within the string and it cannot be
* decoded */
if (len) {
b6b_destroy(l);
return NULL;
}

break;
}

c = b6b_str_copy(s, out);
if (b6b_unlikely(!c)) {
Expand Down
117 changes: 117 additions & 0 deletions tests/b6b_test_decode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* This file is part of b6b.
*
* Copyright 2017 Dima Krasner
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdlib.h>
#include <assert.h>
#include <locale.h>

#include <b6b.h>

int main()
{
struct b6b_interp interp;

setlocale(LC_ALL, "");

assert(b6b_interp_new_argv(&interp, 0, NULL, 0));
assert(b6b_call_copy(&interp, "{\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d}", 10) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, 0));
assert(b6b_call_copy(&interp, "{\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d encode}", 17) == B6B_ERR);
b6b_interp_destroy(&interp);

/* decoding of a Hebrew word should succeed */
assert(b6b_interp_new_argv(&interp, 0, NULL, 0));
assert(b6b_call_copy(&interp, "{\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d decode}", 17) == B6B_OK);
assert(b6b_as_list(interp.fg->_));
assert(!b6b_list_empty(interp.fg->_));
assert(b6b_as_str(b6b_list_first(interp.fg->_)->o));
assert(b6b_list_first(interp.fg->_)->o->slen == 2);
assert(strcmp(b6b_list_first(interp.fg->_)->o->s, "\xd7\xa9") == 0);
assert(b6b_list_next(b6b_list_first(interp.fg->_)));
assert(b6b_as_str(b6b_list_next(b6b_list_first(interp.fg->_))->o));
assert(b6b_list_next(b6b_list_first(interp.fg->_))->o->slen == 2);
assert(strcmp(b6b_list_next(b6b_list_first(interp.fg->_))->o->s, "\xd7\x9c") == 0);
assert(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))));
assert(b6b_as_str(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_)))->o));
assert(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_)))->o->slen == 2);
assert(strcmp(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_)))->o->s, "\xd7\x95") == 0);
assert(b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_)))));
assert(b6b_as_str(b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))))->o));
assert(b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))))->o->slen == 2);
assert(strcmp(b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))))->o->s, "\xd7\x9d") == 0);
assert(!b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))))));
b6b_interp_destroy(&interp);

/* decoding of a string containing a \0 should fail */
assert(b6b_interp_new_argv(&interp, 0, NULL, 0));
assert(b6b_call_copy(&interp, "{\xd7\xa9\xd7\x9c\x00\xd7\x9d decode}", 16) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, 0));
assert(b6b_call_copy(&interp, "{\xd7\xa9\xd7\x9c\xd7\x95\x00 decode}", 16) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, 0));
assert(b6b_call_copy(&interp, "{\xd7\xa9\xd7\x9c\xd7\x95\x00\x9d decode}", 17) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, 0));
assert(b6b_call_copy(&interp, "{\x00\xa9\xd7\x9c\xd7\x95\xd7\x9d decode}", 17) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{\x00a decode}", 12) == B6B_ERR);
b6b_interp_destroy(&interp);

/* decoding of a single-letter Hebrew word should succeed */
assert(b6b_interp_new_argv(&interp, 0, NULL, 0));
assert(b6b_call_copy(&interp, "{\xd7\x90 decode}", 11) == B6B_OK);
assert(b6b_as_list(interp.fg->_));
assert(!b6b_list_empty(interp.fg->_));
assert(b6b_as_str(b6b_list_first(interp.fg->_)->o));
assert(strcmp(b6b_list_first(interp.fg->_)->o->s, "\xd7\x90") == 0);
assert(!b6b_list_next(b6b_list_first(interp.fg->_)));
b6b_interp_destroy(&interp);

/* decoding of a Hebrew word containing an English letter should succeed */
assert(b6b_interp_new_argv(&interp, 0, NULL, 0));
assert(b6b_call_copy(&interp, "{\xd7\xa9\xd7\x9c\x61\xd7\x9d decode}", 16) == B6B_OK);
assert(b6b_as_list(interp.fg->_));
assert(!b6b_list_empty(interp.fg->_));
assert(b6b_as_str(b6b_list_first(interp.fg->_)->o));
assert(b6b_list_first(interp.fg->_)->o->slen == 2);
assert(strcmp(b6b_list_first(interp.fg->_)->o->s, "\xd7\xa9") == 0);
assert(b6b_list_next(b6b_list_first(interp.fg->_)));
assert(b6b_as_str(b6b_list_next(b6b_list_first(interp.fg->_))->o));
assert(b6b_list_next(b6b_list_first(interp.fg->_))->o->slen == 2);
assert(strcmp(b6b_list_next(b6b_list_first(interp.fg->_))->o->s, "\xd7\x9c") == 0);
assert(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))));
assert(b6b_as_str(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_)))->o));
assert(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_)))->o->slen == 1);
assert(strcmp(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_)))->o->s, "a") == 0);
assert(b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_)))));
assert(b6b_as_str(b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))))->o));
assert(b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))))->o->slen == 2);
assert(strcmp(b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))))->o->s, "\xd7\x9d") == 0);
assert(!b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))))));
b6b_interp_destroy(&interp);

return EXIT_SUCCESS;
}
10 changes: 10 additions & 0 deletions tests/b6b_test_try.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ int main()
s = b6b_str_copy("x", 1);
assert(s);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$try}", 6) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$try {}}", 9) == B6B_OK);
assert(b6b_as_str(interp.fg->_));
assert(interp.fg->_->slen == 0);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$try {{$return 5}}}", 20) == B6B_RET);
assert(b6b_as_int(interp.fg->_));
Expand Down
3 changes: 2 additions & 1 deletion tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ tests = [
['bswap', 'quick', 5],
['rand', 'quick', 5],
['socket', 'quick', 5],
['timer', 'quick', 10]
['timer', 'quick', 10],
['decode', 'quick', 5]
]

if get_option('with_threads')
Expand Down

0 comments on commit 189468b

Please sign in to comment.