|
1 | 1 | // system packages: rust-all rust-src |
2 | 2 |
|
| 3 | +#[test] |
| 4 | +fn constants() { |
| 5 | + fn func(arg: i32) -> i32 { |
| 6 | + // immutable: arg += 1; |
| 7 | + arg + 1 // last value is return value |
| 8 | + } |
| 9 | + |
| 10 | + assert_eq!(func(1), 2); |
| 11 | + let logical: bool; |
| 12 | + logical = true; |
| 13 | + assert!(logical); |
| 14 | + |
| 15 | + let int: i32 = 100; |
| 16 | + assert_eq!(int, 100); |
| 17 | +} |
| 18 | + |
| 19 | +#[test] |
| 20 | +fn sample_test() { |
| 21 | + assert_eq!(format!("{}", 0xf), "15"); |
| 22 | + |
| 23 | + let mut variable = 1; // mutable means variable |
| 24 | + |
| 25 | + variable += 1; |
| 26 | + assert_eq!(variable, 2); |
| 27 | +} |
| 28 | + |
| 29 | +#[test] |
| 30 | +fn casting_test() { |
| 31 | + let decimal = 65.4321_f32; |
| 32 | + let integer = decimal as u8; |
| 33 | + let character = integer as char; |
| 34 | + assert_eq!(character, 'A'); |
| 35 | +} |
| 36 | + |
| 37 | +#[test] |
| 38 | +fn types_test() { |
| 39 | + let elem = 5; // i32 by default |
| 40 | + let mut vec = Vec::new(); // generic vector declaration |
| 41 | + vec.push(elem); |
| 42 | + |
| 43 | + // formatting debug representation |
| 44 | + assert_eq!(format!("{:?}", vec), "[5]"); |
| 45 | + |
| 46 | + #[allow(dead_code)] |
| 47 | + type NanoSecond = u64; // type alias |
| 48 | + |
| 49 | + let my_str; |
| 50 | + my_str = "1"; // like char* in C |
| 51 | + |
| 52 | + #[allow(unused_assignments)] |
| 53 | + let mut my_string = String::from(my_str); |
| 54 | + |
| 55 | + my_string = my_str.into(); |
| 56 | + assert_eq!(my_string, "1"); |
| 57 | + |
| 58 | + let s2 = my_string.clone(); |
| 59 | + assert_eq!(s2, "1"); |
| 60 | + |
| 61 | + my_string = "aaa".to_string(); |
| 62 | + assert!(!my_string.is_empty()); |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn parsing_test() { |
| 67 | + // inferred type |
| 68 | + let parsed1: i32 = "5".parse().unwrap(); |
| 69 | + // explicit type |
| 70 | + let parsed2 = "5".parse::<i32>().unwrap(); |
| 71 | + assert_eq!(parsed1, parsed2); |
| 72 | +} |
| 73 | + |
| 74 | +#[test] |
| 75 | +fn controls_test() { |
| 76 | + if 1 > 0 { |
| 77 | + } else { |
| 78 | + }; |
| 79 | + // blocks are expressions, the last expression without ';' is a return value |
| 80 | + let mut res = { |
| 81 | + let a = 1; |
| 82 | + a // without ';' |
| 83 | + }; |
| 84 | + assert_eq!(res, 1); |
| 85 | + assert_eq!({}, ()); // like void return value in C |
| 86 | + |
| 87 | + res = if 1 > 0 { 2 } else { 3 }; |
| 88 | + assert_eq!(res, 2); |
| 89 | + |
| 90 | + // like switch in C |
| 91 | + match true { |
| 92 | + // like case in C |
| 93 | + true => assert!(true), |
| 94 | + _ => assert!(false), |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +#[test] |
| 99 | +fn loops() { |
| 100 | + // match as expression |
| 101 | + let m = match 1 { |
| 102 | + 0 => 0, |
| 103 | + _ => 1, // default match |
| 104 | + }; |
| 105 | + assert_eq!(m, 1); |
| 106 | + |
| 107 | + loop { |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + while false { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + for n in 0..10 { |
| 116 | + assert!(n < 10); |
| 117 | + } |
| 118 | + |
| 119 | + for n in 1..=10 { |
| 120 | + assert!(n <= 10); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[test] |
| 125 | +fn references() { |
| 126 | + let reference1 = &4; |
| 127 | + let ref reference2 = 4; |
| 128 | + // dereferencing |
| 129 | + assert_eq!(*reference1, 4); |
| 130 | + assert_eq!(*reference2, 4); |
| 131 | + let &val = reference1; |
| 132 | + assert_eq!(val, 4); |
| 133 | + |
| 134 | + let s1: &str = "abc"; // like char* |
| 135 | + let ref s2 = *"abc"; // like char* |
| 136 | + assert_eq!(s1, s2); |
| 137 | +} |
| 138 | + |
| 139 | +#[test] |
| 140 | +fn data_structures() { |
| 141 | + // classsic struct |
| 142 | + |
| 143 | + #[allow(dead_code)] |
| 144 | + #[derive(Debug)] // requried for `format!("{:?}", p)` |
| 145 | + struct Person { |
| 146 | + name: String, |
| 147 | + age: u8, |
| 148 | + } |
| 149 | + |
| 150 | + let p = Person { |
| 151 | + name: "Peter".to_string(), |
| 152 | + age: 27, |
| 153 | + }; |
| 154 | + assert_eq!(format!("{:?}", p), "Person { name: \"Peter\", age: 27 }"); |
| 155 | +} |
| 156 | + |
| 157 | +#[test] |
| 158 | +fn tuples() { |
| 159 | + // A tuple struct |
| 160 | + |
| 161 | + #[derive(Debug)] |
| 162 | + struct Pair(i32, f32); |
| 163 | + let pair = Pair(1, 0.1); |
| 164 | + assert_eq!(format!("{:?}", pair), "Pair(1, 0.1)"); |
| 165 | + // destruction (decomposition) |
| 166 | + assert_eq!(pair.0, 1); |
| 167 | + assert_eq!(pair.1, 0.1); |
| 168 | + let Pair(integer, decimal) = pair; |
| 169 | + assert_eq!(integer, 1); |
| 170 | + assert_eq!(decimal, 0.1); |
| 171 | + static GLOBAL: i32 = 1; // statics need to be upppercase |
| 172 | + assert_eq!(GLOBAL, 1); |
| 173 | + |
| 174 | + // generics are like template in C++ |
| 175 | + |
| 176 | + struct SampleGeneric<T>(T); // generic tuple |
| 177 | + let _char = SampleGeneric('a'); |
| 178 | + assert_eq!(_char.0, 'a'); |
| 179 | +} |
| 180 | + |
| 181 | +#[test] |
| 182 | +fn traits() { |
| 183 | + // a trait is like an intrafce, can contain virtual functions |
| 184 | + trait Zero { |
| 185 | + const ZERO: Self; |
| 186 | + fn is_zero(&self) -> bool; |
| 187 | + } |
| 188 | + |
| 189 | + // implemenataions must implement their interface |
| 190 | + impl Zero for i32 { |
| 191 | + const ZERO: Self = 0; |
| 192 | + |
| 193 | + fn is_zero(&self) -> bool { |
| 194 | + *self == Self::ZERO |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + assert_eq!(i32::ZERO, 0); |
| 199 | + assert!(i32::ZERO.is_zero()); |
| 200 | + assert!(!4.is_zero()); |
| 201 | +} |
| 202 | + |
| 203 | +#[test] |
| 204 | +fn destructor_test() { |
| 205 | + static mut DROPPED: bool = false; |
| 206 | + struct Droppper; |
| 207 | + impl Drop for Droppper { |
| 208 | + // destructor |
| 209 | + fn drop(&mut self) { |
| 210 | + unsafe { |
| 211 | + // because `static mut` is unsafe |
| 212 | + DROPPED = true; // must be static (global) |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + { |
| 217 | + let _x = Droppper; |
| 218 | + } |
| 219 | + unsafe { assert!(DROPPED) }; |
| 220 | +} |
| 221 | + |
| 222 | +#[test] |
| 223 | +fn options() { |
| 224 | + let x: Option<u32> = Some(2); |
| 225 | + assert_eq!(x.is_some(), true); |
| 226 | + |
| 227 | + let x: Option<u32> = None; |
| 228 | + assert_eq!(x.is_some(), false); |
| 229 | +} |
| 230 | + |
| 231 | +#[test] |
| 232 | +fn raw_pointers() { |
| 233 | + let mut var: i32 = 3; |
| 234 | + let ptr: *mut i32 = &mut var; |
| 235 | + unsafe { |
| 236 | + if !ptr.is_null() { |
| 237 | + assert_eq!(*ptr, 3); |
| 238 | + } |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +#[test] |
| 243 | +fn mutable_function_argument() { |
| 244 | + let mut a = String::from("a"); |
| 245 | + fn append(arg: &mut String, s: &str) { |
| 246 | + arg.push_str(s); |
| 247 | + // immutable: arg += 1; |
| 248 | + //arg + 1 // last value is return value |
| 249 | + } |
| 250 | + append(&mut a, "b"); |
| 251 | + append(&mut a, "c"); |
| 252 | + assert_eq!(a, "abc"); |
| 253 | +} |
| 254 | + |
| 255 | +#[test] |
| 256 | +#[ignore] |
| 257 | +fn failing_test() { |
| 258 | + assert!(false) |
| 259 | +} |
| 260 | + |
3 | 261 | fn main() { |
4 | 262 | print!("output"); |
5 | 263 | println!(" + new line"); |
|
0 commit comments