Skip to content

Commit

Permalink
add tests for stdlib/ascii.jou (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli authored Dec 3, 2023
1 parent 602bfd9 commit 334ed80
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions self_hosted/runs_wrong.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ tests/wrong_type/assert.jou
tests/wrong_type/cannot_be_indexed.jou
tests/wrong_type/index.jou
stdlib/ascii.jou
tests/should_succeed/ascii_test.jou
2 changes: 1 addition & 1 deletion src/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void print_string(const char *s, int len)
else if (s[i] == '\n')
printf("\\n");
else
printf("\\x%02x", s[i]); // TODO: \x is not yet recognized by the tokenizer
printf("\\x%02x", (unsigned char)s[i]); // TODO: \x is not yet recognized by the tokenizer
}
putchar('"');
}
Expand Down
8 changes: 2 additions & 6 deletions stdlib/ascii.jou
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@ import "stdlib/str.jou"
import "stdlib/mem.jou"

# Test if a string is ASCII only.
# TODO: test
def is_ascii(s: byte*) -> bool:
for p = s; *p != '\0'; p++:
if *p >= 128:
return False
return True

# Check for '0', '1', ..., '9'.
# TODO: test
def is_ascii_digit(b: byte) -> bool:
return '0' <= b and b <= '9'

# Checks if the given byte is an ASCII punctuation character, such as '*' or '_' or '"'.
# TODO: test
def is_ascii_punctuation(b: byte) -> bool:
return strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", b) != NULL
return b != '\0' and strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", b) != NULL

# Checks if the given byte is an ASCII whitespace character, such as ' ' or '\n'.
# TODO: test
def is_ascii_whitespace(b: byte) -> bool:
return strchr("\t\n\x0b\x0c\r\x1c\x1d\x1e\x1f ", b) != NULL
return b != '\0' and strchr("\t\n\x0b\x0c\r\x1c\x1d\x1e\x1f ", b) != NULL

# Removes ASCII whitespace from both ends of a string in-place.
# Similar to .strip() in Python or .trim() in JavaScript.
Expand Down
35 changes: 35 additions & 0 deletions tests/should_succeed/ascii_test.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import "stdlib/ascii.jou"
import "stdlib/str.jou"

def main() -> int:
assert is_ascii("hello")
assert is_ascii("hello world test!!! @$!")
assert is_ascii("")
assert not is_ascii("örkkimörkki")

assert is_ascii_digit('0')
assert is_ascii_digit('7')
assert is_ascii_digit('9')
assert not is_ascii_digit('x')
assert not is_ascii_digit('\0')

assert is_ascii_punctuation('!')
assert is_ascii_punctuation('_')
assert not is_ascii_punctuation('a')
assert not is_ascii_punctuation('2')
assert not is_ascii_punctuation('\0')

assert is_ascii_whitespace(' ')
assert is_ascii_whitespace('\t')
assert is_ascii_whitespace('\r')
assert is_ascii_whitespace('\n')
assert not is_ascii_whitespace('a')
assert not is_ascii_whitespace('.')
assert not is_ascii_whitespace('\0')

s: byte[100]
strcpy(s, " hello world \r\n \t ")
trim_ascii_whitespace(s)
assert strcmp(s, "hello world") == 0

return 0

0 comments on commit 334ed80

Please sign in to comment.