From d660366368f905dedd1aaa895ab9e2551b1e7dc7 Mon Sep 17 00:00:00 2001 From: Maksim Bondarenkov Date: Wed, 24 Jul 2024 20:04:00 +0300 Subject: [PATCH] add test target for rusted --- dad-is-great-in-C/meson.build | 48 +++++++++++++++--------------- meson.build | 49 ++++++++++++++++++++----------- subprojects/rusted/Cargo.toml | 4 +-- subprojects/rusted/src/lib.rs | 55 +++++++++++++++++++++++++++++------ 4 files changed, 104 insertions(+), 52 deletions(-) diff --git a/dad-is-great-in-C/meson.build b/dad-is-great-in-C/meson.build index 91679b0..af3f25b 100644 --- a/dad-is-great-in-C/meson.build +++ b/dad-is-great-in-C/meson.build @@ -2,38 +2,38 @@ cc = meson.get_compiler('c') cxx = meson.get_compiler('cpp') add_project_arguments( - cc.get_supported_arguments('-fdiagnostics-color=always', '-pipe'), - language: 'c', + cc.get_supported_arguments('-fdiagnostics-color=always', '-pipe'), + language: 'c', ) add_project_arguments( - cxx.get_supported_arguments( - '-Wno-c++98-compat-pedantic', - '-Wno-unsafe-buffer-usage', - '-Wno-poison-system-directories', - '-fdiagnostics-color=always', - '-pipe' - ), - language: 'cpp', + cxx.get_supported_arguments( + '-Wno-c++98-compat-pedantic', + '-Wno-unsafe-buffer-usage', + '-Wno-poison-system-directories', + '-fdiagnostics-color=always', + '-pipe', + ), + language: 'cpp', ) src_c = [ - 'b_sort.cpp', - 'firstword.cpp', - 'hords.cpp', - 'i_sort.cpp', - 'lastword.cpp', - 'longman.cpp', - 'm_sort.cpp', - 'middles.cpp', - 'resheto.cpp', - 's_sort.cpp', - 'speedometer++.cpp', - 'speedometer.c', - 'tumba-umba.cpp', + 'b_sort.cpp', + 'firstword.cpp', + 'hords.cpp', + 'i_sort.cpp', + 'lastword.cpp', + 'longman.cpp', + 'm_sort.cpp', + 'middles.cpp', + 'resheto.cpp', + 's_sort.cpp', + 'speedometer++.cpp', + 'speedometer.c', + 'tumba-umba.cpp', ] foreach file : src_c - executable(file.split('.')[0], file) + executable(file.split('.')[0], file) endforeach if tests diff --git a/meson.build b/meson.build index 8c7d0e5..0c13642 100644 --- a/meson.build +++ b/meson.build @@ -1,16 +1,16 @@ project( - 'ognevny-my-code', - version: '0.0.0', - meson_version: '>= 1.5.0', - default_options: [ - 'warning_level=everything', - 'cpp_std=gnu++11,c++11', - 'c_std=gnu11,c11', - 'rust_std=2021', - 'crossbeam-utils-0.8-rs:feature-default=true', - 'aho-corasick-1-rs:feature-perf-literal=true', - ], - license: 'WTFPL', + 'ognevny-my-code', + version: '0.0.0', + meson_version: '>= 1.5.0', + default_options: [ + 'warning_level=everything', + 'cpp_std=gnu++11,c++11', + 'c_std=gnu11,c11', + 'rust_std=2021', + 'crossbeam-utils-0.8-rs:feature-default=true', + 'aho-corasick-1-rs:feature-perf-literal=true', + ], + license: 'WTFPL', ) build_c = get_option('build_c_cpp') @@ -20,15 +20,30 @@ build_all = get_option('build_all') tests = get_option('tests') if build_c or build_all - add_languages(['c', 'cpp'], native: false) - subdir('dad-is-great-in-C') + add_languages(['c', 'cpp'], native: false) + subdir('dad-is-great-in-C') endif if build_rust or build_all - add_languages(['rust'], native: false) - subproject('rusted') + add_languages(['rust'], native: false) + subproject('rusted') + if tests + custom_target( + 'cargo-test', + build_by_default: true, + build_always_stale: true, + console: true, + output: 'rusted-tests', + command: [ + find_program('cargo'), + 'test', + '--manifest-path', + meson.project_source_root() / 'subprojects/rusted/Cargo.toml', + ], + ) + endif endif if build_py or build_all - subdir('pie') + subdir('pie') endif diff --git a/subprojects/rusted/Cargo.toml b/subprojects/rusted/Cargo.toml index 4cd9952..3c4ab17 100644 --- a/subprojects/rusted/Cargo.toml +++ b/subprojects/rusted/Cargo.toml @@ -2,6 +2,7 @@ name = "rusted" version = "0.1.0" edition = "2021" +rust-version = "1.65.0" [dependencies] itertools = "0.13" @@ -18,6 +19,5 @@ meval = { git = "https://github.com/Titaniumtown/meval-rs" } name = "rusted" path = "src/lib.rs" test = true +doctest = false doc = true -proc-macro = false -edition = "2021" diff --git a/subprojects/rusted/src/lib.rs b/subprojects/rusted/src/lib.rs index bf6784b..0705f43 100644 --- a/subprojects/rusted/src/lib.rs +++ b/subprojects/rusted/src/lib.rs @@ -4,8 +4,8 @@ pub mod ege1 { use std::fs; /// the first code with tasks from EGE exam - pub fn ex1() -> Result> { - let text = fs::read_to_string("11.txt")?; + pub fn ex1() -> usize { + let text = fs::read_to_string("11.txt").unwrap(); let newtext = text.replace("INFINITY", "@"); let mut pos = Vec::new(); for (i, char) in newtext.chars().enumerate() { @@ -17,16 +17,16 @@ pub mod ege1 { for i in 0..(pos.len() - 1001) { mx = mx.max(pos[i + 1001] - pos[i] - 1 + 7000 + 14); } - Ok(mx) + mx } #[cfg(test)] mod tests { - use crate::*; + use super::ex1; #[test] fn test_all() { - let ex1 = ex1().unwrap(); + let ex1 = ex1(); assert_eq!(ex1, 36747); } } @@ -39,6 +39,18 @@ pub mod first_word { .map_or_else(|| input.trim(), |n| input[..n].trim()) .to_owned() } + + #[cfg(test)] + mod tests { + use super::first_word; + + #[test] + fn test_all() { + assert_eq!(first_word("hello world "), "hello"); + assert_eq!(first_word(" no"), ""); + assert_eq!(first_word("coffee of tea"), "coffee"); + } + } } pub mod integral { @@ -163,6 +175,18 @@ pub mod last_word { input[n + 1..].trim().to_owned() }) } + + #[cfg(test)] + mod tests { + use super::last_word; + + #[test] + fn test_all() { + assert_eq!(last_word("hello world "), "world"); + assert_eq!(last_word(" no"), "no"); + assert_eq!(last_word("coffee of tea"), "tea"); + } + } } pub mod longman { @@ -184,6 +208,7 @@ pub mod longman2 { /// find a maximum HEX number in string pub fn longman2(data: &str) -> String { + assert!(data.is_ascii(), "non-ASCII characters are not allowed"); let nums: Vec = data .chars() .fold((String::new(), vec![]), |(mut word, mut nums), char| { @@ -204,6 +229,16 @@ pub mod longman2 { .max_by_key(|num| usize::from_str_radix(num, 16).unwrap()) .unwrap() } + + #[cfg(test)] + mod tests { + use super::longman2; + + #[test] + fn test_all() { + assert_eq!(longman2("af5Y3d"), "af5"); + } + } } pub mod mask1 { @@ -219,7 +254,7 @@ pub mod mask1 { #[cfg(test)] mod tests { - use crate::*; + use super::fn_match; #[test] fn test_all() { @@ -370,7 +405,7 @@ pub mod mcko { #[cfg(test)] mod tests { - use crate::*; + use super::*; #[test] fn test_all() { @@ -432,7 +467,7 @@ pub mod probnik { #[cfg(test)] mod tests { - use crate::*; + use super::probnik; #[test] fn test_all() { @@ -550,7 +585,7 @@ pub mod speedometer { #[inline] #[allow(dead_code)] - const fn s() -> u32 { + pub const fn s() -> u32 { let mut n = 1; while n < 1_000_000_000 { n += 1; @@ -560,6 +595,8 @@ pub mod speedometer { #[cfg(test)] mod tests { + use super::s; + #[test] fn print() { print!("{}", s())