Skip to content

Commit

Permalink
Add some tests for unicode.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVeryDarkness committed Aug 15, 2024
1 parent 812c457 commit e3c2fb7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
8 changes: 8 additions & 0 deletions examples/string_reverse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use iof::read_line;

/// Reads two lines of strings from input, and checks if they are in reverse order.
fn main() {
let a: String = read_line();
let b: String = read_line();
assert_eq!(a, b.chars().rev().collect::<String>());
}
27 changes: 23 additions & 4 deletions tests/stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,44 @@ use std::{
process::{Command, Stdio},
};

#[test]
fn dot_product() {
fn test_example(name: &str, input: &str) {
let mut child = Command::new("cargo")
.arg("run")
.arg("--example")
.arg("dot_product")
.arg(name)
.stdin(Stdio::piped())
.spawn()
.unwrap();
child
.stdin
.as_mut()
.unwrap()
.write_all("3\n1 2 3\n4 5 6\n4 10 18".as_bytes())
.write_all(input.as_bytes())
.unwrap();
let status = child.wait().unwrap();
assert!(status.success());
}

#[test]
fn dot_product() {
test_example("dot_product", "3\n1 2 3\n4 5 6\n4 10 18");
}

/// Reads two lines of strings from input, and checks if they are in reverse order.
///
/// ```python
/// assert read_line() == read_line()[::-1]
/// ```
#[test]
fn string_reverse() {
// καλημέραHello,你好!🦀
// 🦀!好你,olleHαρέμηλακ
test_example(
"string_reverse",
"καλημέραHello,你好!🦀\n🦀!好你,olleHαρέμηλακ",
);
}

#[test]
fn read_n_0() {
let s1: Vec<usize> = read_n(0);
Expand Down
40 changes: 40 additions & 0 deletions tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,43 @@ fn read_line_failure() {

let _: String = reader.read_line();
}

#[test]
fn read_unicode() {
let reader = Cursor::new("🦀🦀🦀 Rust 你好! καλημέρα ".as_bytes());
let mut reader = InputStream::new(reader);

let s: String = reader.read();
assert_eq!(s, "🦀🦀🦀");

let s: String = reader.read();
assert_eq!(s, "Rust");

let s: String = reader.read();
assert_eq!(s, "你好!");

let s: String = reader.read();
assert_eq!(s, "καλημέρα");

assert!(iof::ReadInto::<String>::try_read(&mut reader).is_err());
}

#[test]
fn read_line_unicode() {
let reader = Cursor::new("🦀🦀🦀\nRust \n 你好!\n καλημέρα ".as_bytes());
let mut reader = InputStream::new(reader);

let s: String = reader.read_line();
assert_eq!(s, "🦀🦀🦀");

let s: String = reader.read_line();
assert_eq!(s, "Rust");

let s: String = reader.read_line();
assert_eq!(s, "你好!");

let s: String = reader.read_line();
assert_eq!(s, "καλημέρα");

assert!(iof::ReadInto::<String>::try_read_line(&mut reader).is_err());
}

0 comments on commit e3c2fb7

Please sign in to comment.