compiler/
lib/
examples/
std/
arch/
char/
collections/
lang/
sys/
arch.rad
68 B
char.rad
855 B
collections.rad
39 B
fmt.rad
8.1 KiB
intrinsics.rad
467 B
io.rad
1.3 KiB
lang.rad
276 B
mem.rad
2.2 KiB
sys.rad
173 B
testing.rad
2.4 KiB
tests.rad
12.9 KiB
vec.rad
1.7 KiB
std.rad
281 B
scripts/
seed/
sublime/
test/
vim/
.gitignore
336 B
.gitsigners
112 B
LICENSE
1.1 KiB
Makefile
3.7 KiB
README
4.8 KiB
STYLE
2.5 KiB
std.lib
1.2 KiB
std.lib.test
347 B
lib/std/tests.rad
raw
| 1 | //! Tests for formatting functions. |
| 2 | |
| 3 | use std::fmt; |
| 4 | use std::mem; |
| 5 | use std::vec; |
| 6 | use std::testing; |
| 7 | use std::sys::unix; |
| 8 | |
| 9 | // fmt ///////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | @test fn testFormatU32Zero() throws (testing::TestError) { |
| 12 | let mut buffer: [u8; 11] = [0; 11]; |
| 13 | let result: *[u8] = fmt::formatU32(0, &mut buffer[..]); |
| 14 | try testing::expect(result.len == 1); |
| 15 | try testing::expectBytesEq(result, "0"); |
| 16 | } |
| 17 | |
| 18 | @test fn testFormatU32Basic() throws (testing::TestError) { |
| 19 | let mut buffer: [u8; 11] = [0; 11]; |
| 20 | let result: *[u8] = fmt::formatU32(123, &mut buffer[..]); |
| 21 | try testing::expect(result.len == 3); |
| 22 | try testing::expectBytesEq(result, "123"); |
| 23 | } |
| 24 | |
| 25 | @test fn testFormatU32Large() throws (testing::TestError) { |
| 26 | let mut buffer: [u8; 11] = [0; 11]; |
| 27 | let result: *[u8] = fmt::formatU32(90000, &mut buffer[..]); |
| 28 | try testing::expect(result.len == 5); |
| 29 | try testing::expectBytesEq(result, "90000"); |
| 30 | } |
| 31 | |
| 32 | @test fn testFormatU32Max() throws (testing::TestError) { |
| 33 | let mut buffer: [u8; 11] = [0; 11]; |
| 34 | let result: *[u8] = fmt::formatU32(4294967295, &mut buffer[..]); |
| 35 | try testing::expect(result.len == 10); |
| 36 | try testing::expectBytesEq(result, "4294967295"); |
| 37 | } |
| 38 | |
| 39 | @test fn testFormatI32Positive() throws (testing::TestError) { |
| 40 | let mut buffer: [u8; 11] = [0; 11]; |
| 41 | let result: *[u8] = fmt::formatI32(456, &mut buffer[..]); |
| 42 | try testing::expect(result.len == 3); |
| 43 | try testing::expectBytesEq(result, "456"); |
| 44 | } |
| 45 | |
| 46 | @test fn testFormatI32Negative() throws (testing::TestError) { |
| 47 | let mut buffer: [u8; 11] = [0; 11]; |
| 48 | let result: *[u8] = fmt::formatI32(-789, &mut buffer[..]); |
| 49 | try testing::expect(result.len == 4); |
| 50 | try testing::expectBytesEq(result, "-789"); |
| 51 | } |
| 52 | |
| 53 | @test fn testFormatI32Zero() throws (testing::TestError) { |
| 54 | let mut buffer: [u8; 11] = [0; 11]; |
| 55 | let result: *[u8] = fmt::formatI32(0, &mut buffer[..]); |
| 56 | try testing::expect(result.len == 1); |
| 57 | try testing::expectBytesEq(result, "0"); |
| 58 | } |
| 59 | |
| 60 | @test fn testFormatI32Max() throws (testing::TestError) { |
| 61 | let mut buffer: [u8; 11] = [0; 11]; |
| 62 | let result: *[u8] = fmt::formatI32(2147483647, &mut buffer[..]); |
| 63 | try testing::expect(result.len == 10); |
| 64 | try testing::expectBytesEq(result, "2147483647"); |
| 65 | } |
| 66 | |
| 67 | @test fn testFormatI32Min() throws (testing::TestError) { |
| 68 | let mut buffer: [u8; 11] = [0; 11]; |
| 69 | let result: *[u8] = fmt::formatI32(-2147483648, &mut buffer[..]); |
| 70 | try testing::expect(result.len == 11); |
| 71 | try testing::expectBytesEq(result, "-2147483648"); |
| 72 | } |
| 73 | |
| 74 | @test fn testFormatU64Zero() throws (testing::TestError) { |
| 75 | let mut buffer: [u8; 20] = [0; 20]; |
| 76 | let result: *[u8] = fmt::formatU64(0, &mut buffer[..]); |
| 77 | try testing::expect(result.len == 1); |
| 78 | try testing::expectBytesEq(result, "0"); |
| 79 | } |
| 80 | |
| 81 | @test fn testFormatU64Max() throws (testing::TestError) { |
| 82 | let mut buffer: [u8; 20] = [0; 20]; |
| 83 | let result: *[u8] = fmt::formatU64(18446744073709551615, &mut buffer[..]); |
| 84 | try testing::expect(result.len == 20); |
| 85 | try testing::expectBytesEq(result, "18446744073709551615"); |
| 86 | } |
| 87 | |
| 88 | @test fn testFormatI64Negative() throws (testing::TestError) { |
| 89 | let mut buffer: [u8; 20] = [0; 20]; |
| 90 | let result: *[u8] = fmt::formatI64(-9876543210, &mut buffer[..]); |
| 91 | try testing::expect(result.len == 11); |
| 92 | try testing::expectBytesEq(result, "-9876543210"); |
| 93 | } |
| 94 | |
| 95 | @test fn testFormatI64Min() throws (testing::TestError) { |
| 96 | let mut buffer: [u8; 20] = [0; 20]; |
| 97 | let result: *[u8] = fmt::formatI64(-9223372036854775808, &mut buffer[..]); |
| 98 | try testing::expect(result.len == 20); |
| 99 | try testing::expectBytesEq(result, "-9223372036854775808"); |
| 100 | } |
| 101 | |
| 102 | @test fn testParseIntLiteralText() throws (testing::TestError) { |
| 103 | let dec = try fmt::parseInt("123") catch { |
| 104 | throw testing::TestError::Failed; |
| 105 | }; |
| 106 | try testing::expect(dec.magnitude == 123); |
| 107 | try testing::expect(dec.radix == fmt::Radix::Decimal); |
| 108 | |
| 109 | let hex = try fmt::parseInt("0x2a") catch { |
| 110 | throw testing::TestError::Failed; |
| 111 | }; |
| 112 | try testing::expect(hex.magnitude == 42); |
| 113 | try testing::expect(hex.radix == fmt::Radix::Hex); |
| 114 | |
| 115 | let bin = try fmt::parseInt("0b101") catch { |
| 116 | throw testing::TestError::Failed; |
| 117 | }; |
| 118 | try testing::expect(bin.magnitude == 5); |
| 119 | try testing::expect(bin.radix == fmt::Radix::Binary); |
| 120 | } |
| 121 | |
| 122 | @test fn testDigitFromAscii() throws (testing::TestError) { |
| 123 | let zero = fmt::digitFromAscii('0', 10) else throw testing::TestError::Failed; |
| 124 | try testing::expect(zero == 0); |
| 125 | |
| 126 | let nine = fmt::digitFromAscii('9', 10) else throw testing::TestError::Failed; |
| 127 | try testing::expect(nine == 9); |
| 128 | |
| 129 | let lower = fmt::digitFromAscii('a', 16) else throw testing::TestError::Failed; |
| 130 | try testing::expect(lower == 10); |
| 131 | |
| 132 | let lowerF = fmt::digitFromAscii('f', 16) else throw testing::TestError::Failed; |
| 133 | try testing::expect(lowerF == 15); |
| 134 | |
| 135 | let upper = fmt::digitFromAscii('A', 16) else throw testing::TestError::Failed; |
| 136 | try testing::expect(upper == 10); |
| 137 | |
| 138 | let upperF = fmt::digitFromAscii('F', 16) else throw testing::TestError::Failed; |
| 139 | try testing::expect(upperF == 15); |
| 140 | |
| 141 | try testing::expect(fmt::digitFromAscii('g', 16) == nil); |
| 142 | try testing::expect(fmt::digitFromAscii('_', 10) == nil); |
| 143 | } |
| 144 | |
| 145 | @test fn testParseIntLiteralTextErrors() throws (testing::TestError) { |
| 146 | try fmt::parseInt("") catch { |
| 147 | return; |
| 148 | }; |
| 149 | throw testing::TestError::Failed; |
| 150 | } |
| 151 | |
| 152 | @test fn testParseIntLiteralTextRejectsSigns() throws (testing::TestError) { |
| 153 | try fmt::parseInt("-1") catch { |
| 154 | try fmt::parseInt("+1") catch { |
| 155 | return; |
| 156 | }; |
| 157 | }; |
| 158 | throw testing::TestError::Failed; |
| 159 | } |
| 160 | |
| 161 | @test fn testParseIntLiteralTextInvalidDigitErrors() throws (testing::TestError) { |
| 162 | try fmt::parseInt("0b2") catch { |
| 163 | return; |
| 164 | }; |
| 165 | throw testing::TestError::Failed; |
| 166 | } |
| 167 | |
| 168 | @test fn testParseIntLiteralTextOverflowErrors() throws (testing::TestError) { |
| 169 | try fmt::parseInt("18446744073709551616") catch { |
| 170 | return; |
| 171 | }; |
| 172 | throw testing::TestError::Failed; |
| 173 | } |
| 174 | |
| 175 | @test fn testParseCharLiteralText() throws (testing::TestError) { |
| 176 | let x = try fmt::parseChar("'x'") catch { |
| 177 | throw testing::TestError::Failed; |
| 178 | }; |
| 179 | try testing::expect(x == 'x'); |
| 180 | |
| 181 | let newline = try fmt::parseChar("'\\n'") catch { |
| 182 | throw testing::TestError::Failed; |
| 183 | }; |
| 184 | try testing::expect(newline == '\n'); |
| 185 | } |
| 186 | |
| 187 | @test fn testParseCharLiteralTextErrors() throws (testing::TestError) { |
| 188 | try fmt::parseChar("''") catch { |
| 189 | return; |
| 190 | }; |
| 191 | throw testing::TestError::Failed; |
| 192 | } |
| 193 | |
| 194 | @test fn testUnescapeString() throws (testing::TestError) { |
| 195 | let mut buffer: [u8; 8] = [0; 8]; |
| 196 | let len = fmt::unescapeString("a\\n\\0", &mut buffer[..]); |
| 197 | |
| 198 | try testing::expect(len == 3); |
| 199 | try testing::expect(buffer[0] == 'a'); |
| 200 | try testing::expect(buffer[1] == '\n'); |
| 201 | try testing::expect(buffer[2] == 0); |
| 202 | } |
| 203 | |
| 204 | // mem ///////////////////////////////////////////////////////////////////////// |
| 205 | |
| 206 | @test fn testCopyFullSlice() throws (testing::TestError) { |
| 207 | let mut xs: [u8; 3] = [1, 2, 3]; |
| 208 | let mut ys: [u8; 3] = [4, 5, 6]; |
| 209 | |
| 210 | try! mem::copy(&mut xs[..], &ys[..]); |
| 211 | try testing::expectBytesEq(&xs[..], &ys[..]); |
| 212 | try testing::expectBytesEq(&xs[..], &[4, 5, 6]); |
| 213 | } |
| 214 | |
| 215 | @test fn testCopyPartialSlice() throws (testing::TestError) { |
| 216 | let mut xs: [u8; 3] = [1, 2, 3]; |
| 217 | |
| 218 | try! mem::copy(&mut xs[..2], &[8, 9]); |
| 219 | try testing::expectBytesEq(&xs[..], &[8, 9, 3]); |
| 220 | } |
| 221 | |
| 222 | @test fn testCopySingleElement() throws (testing::TestError) { |
| 223 | let mut xs: [u8; 3] = [1, 2, 3]; |
| 224 | |
| 225 | try! mem::copy(&mut xs[..], &[7]); |
| 226 | try testing::expectBytesEq(&xs[..], &[7, 2, 3]); |
| 227 | } |
| 228 | |
| 229 | @test fn testCopyBufferTooSmallError() throws (testing::TestError) { |
| 230 | let mut xs: [u8; 2] = [1, 2]; |
| 231 | let ys: [u8; 3] = [4, 5, 6]; |
| 232 | |
| 233 | let mut threw = false; |
| 234 | try mem::copy(&mut xs[..], &ys[..]) catch { |
| 235 | set threw = true; |
| 236 | }; |
| 237 | try testing::expect(threw); |
| 238 | } |
| 239 | |
| 240 | @test fn testWriteFileParts() throws (testing::TestError) { |
| 241 | let path = "/tmp/radiance-std-write-file-parts.test"; |
| 242 | let mut buffer: [u8; 16] = undefined; |
| 243 | |
| 244 | if not unix::writeFileParts(path, &["hello", " ", "world"]) { |
| 245 | throw testing::TestError::Failed; |
| 246 | } |
| 247 | let data = unix::readFile(path, &mut buffer[..]) else { |
| 248 | throw testing::TestError::Failed; |
| 249 | }; |
| 250 | try testing::expectBytesEq(data, "hello world"); |
| 251 | } |
| 252 | |
| 253 | @test fn testStripPrefixMatch() throws (testing::TestError) { |
| 254 | let input: *[u8] = "hello world"; |
| 255 | let prefix: *[u8] = "hello"; |
| 256 | |
| 257 | let result = mem::stripPrefix(prefix, input) else { |
| 258 | throw testing::TestError::Failed; |
| 259 | }; |
| 260 | try testing::expectBytesEq(result, " world"); |
| 261 | } |
| 262 | |
| 263 | @test fn testStripPrefixNoMatch() throws (testing::TestError) { |
| 264 | let input: *[u8] = "hello world"; |
| 265 | let prefix: *[u8] = "goodbye"; |
| 266 | |
| 267 | let result = mem::stripPrefix(prefix, input); |
| 268 | try testing::expect(result == nil); |
| 269 | } |
| 270 | |
| 271 | @test fn testStripPrefixEmpty() throws (testing::TestError) { |
| 272 | let input: *[u8] = "hello"; |
| 273 | let prefix: *[u8] = ""; |
| 274 | |
| 275 | let result = mem::stripPrefix(prefix, input) else { |
| 276 | throw testing::TestError::Failed; |
| 277 | }; |
| 278 | try testing::expectBytesEq(result, input); |
| 279 | } |
| 280 | |
| 281 | @test fn testStripPrefixTooLong() throws (testing::TestError) { |
| 282 | let input: *[u8] = "hi"; |
| 283 | let prefix: *[u8] = "hello"; |
| 284 | |
| 285 | let result = mem::stripPrefix(prefix, input); |
| 286 | try testing::expect(result == nil); |
| 287 | } |
| 288 | |
| 289 | @test fn testEqMatchingSlices() throws (testing::TestError) { |
| 290 | let a: *[u8] = "abc"; |
| 291 | let b: *[u8] = "abc"; |
| 292 | |
| 293 | try testing::expect(mem::eq(a, b)); |
| 294 | } |
| 295 | |
| 296 | @test fn testEqDifferentContents() throws (testing::TestError) { |
| 297 | let a: *[u8] = "abc"; |
| 298 | let b: *[u8] = "abd"; |
| 299 | |
| 300 | try testing::expectNot(mem::eq(a, b)); |
| 301 | } |
| 302 | |
| 303 | @test fn testEqDifferentLengths() throws (testing::TestError) { |
| 304 | let a: *[u8] = "abc"; |
| 305 | let b: *[u8] = "abcd"; |
| 306 | |
| 307 | try testing::expectNot(mem::eq(a, b)); |
| 308 | } |
| 309 | |
| 310 | @test fn testEqEmptySlices() throws (testing::TestError) { |
| 311 | let a: *[u8] = ""; |
| 312 | let b: *[u8] = ""; |
| 313 | |
| 314 | try testing::expect(mem::eq(a, b)); |
| 315 | } |
| 316 | |
| 317 | @test fn testCmpEqualSlices() throws (testing::TestError) { |
| 318 | try testing::expect(mem::cmp("abc", "abc") == 0); |
| 319 | } |
| 320 | |
| 321 | @test fn testCmpLessThanByContent() throws (testing::TestError) { |
| 322 | try testing::expect(mem::cmp("abc", "abd") < 0); |
| 323 | } |
| 324 | |
| 325 | @test fn testCmpGreaterThanByContent() throws (testing::TestError) { |
| 326 | try testing::expect(mem::cmp("abd", "abc") > 0); |
| 327 | } |
| 328 | |
| 329 | @test fn testCmpPrefixOrdering() throws (testing::TestError) { |
| 330 | try testing::expect(mem::cmp("abc", "abcd") < 0); |
| 331 | try testing::expect(mem::cmp("abcd", "abc") > 0); |
| 332 | } |
| 333 | |
| 334 | // vec ///////////////////////////////////////////////////////////////////////// |
| 335 | |
| 336 | /// Mixed-width value used by vector tests. |
| 337 | record Obj { |
| 338 | /// Wide field. |
| 339 | wide: u64, |
| 340 | /// Narrow field. |
| 341 | narrow: u8, |
| 342 | } |
| 343 | |
| 344 | instantiate vec::Vec⟨i32⟩, |
| 345 | vec::init⟨i32⟩, |
| 346 | vec::len⟨i32⟩, |
| 347 | vec::capacity⟨i32⟩, |
| 348 | vec::reset⟨i32⟩, |
| 349 | vec::get⟨i32⟩, |
| 350 | vec::push⟨i32⟩, |
| 351 | vec::put⟨i32⟩, |
| 352 | vec::pop⟨i32⟩; |
| 353 | |
| 354 | instantiate vec::Vec⟨Obj⟩, |
| 355 | vec::init⟨Obj⟩, |
| 356 | vec::get⟨Obj⟩, |
| 357 | vec::push⟨Obj⟩, |
| 358 | vec::put⟨Obj⟩; |
| 359 | |
| 360 | @test fn testVecInitialState() throws (testing::TestError) { |
| 361 | let mut arena: [i32; 4] = undefined; |
| 362 | let mut v: vec::Vec⟨i32⟩ = undefined; |
| 363 | vec::init⟨i32⟩(&mut v, &mut arena[..]); |
| 364 | |
| 365 | assert vec::len⟨i32⟩(&v) == 0; |
| 366 | assert vec::capacity⟨i32⟩(&v) == 4; |
| 367 | } |
| 368 | |
| 369 | @test fn testVecGetPut() throws (testing::TestError) { |
| 370 | let mut arena: [i32; 3] = undefined; |
| 371 | let mut v: vec::Vec⟨i32⟩ = undefined; |
| 372 | vec::init⟨i32⟩(&mut v, &mut arena[..]); |
| 373 | |
| 374 | assert vec::push⟨i32⟩(&mut v, 10); |
| 375 | assert vec::push⟨i32⟩(&mut v, 20); |
| 376 | assert vec::push⟨i32⟩(&mut v, 30); |
| 377 | assert not vec::push⟨i32⟩(&mut v, 40); |
| 378 | |
| 379 | let first = vec::get⟨i32⟩(&v, 0) |
| 380 | else throw testing::TestError::Failed; |
| 381 | assert *first == 10; |
| 382 | assert vec::get⟨i32⟩(&v, 3) == nil; |
| 383 | |
| 384 | assert vec::put⟨i32⟩(&mut v, 1, 999); |
| 385 | let replaced = vec::get⟨i32⟩(&v, 1) |
| 386 | else throw testing::TestError::Failed; |
| 387 | assert *replaced == 999; |
| 388 | assert not vec::put⟨i32⟩(&mut v, 3, 1); |
| 389 | } |
| 390 | |
| 391 | @test fn testVecPopReset() throws (testing::TestError) { |
| 392 | let mut arena: [i32; 2] = undefined; |
| 393 | let mut v: vec::Vec⟨i32⟩ = undefined; |
| 394 | vec::init⟨i32⟩(&mut v, &mut arena[..]); |
| 395 | |
| 396 | assert vec::push⟨i32⟩(&mut v, 42); |
| 397 | assert vec::push⟨i32⟩(&mut v, 100); |
| 398 | assert vec::pop⟨i32⟩(&mut v) == 100; |
| 399 | assert vec::pop⟨i32⟩(&mut v) == 42; |
| 400 | assert vec::pop⟨i32⟩(&mut v) == nil; |
| 401 | |
| 402 | assert vec::push⟨i32⟩(&mut v, 7); |
| 403 | vec::reset⟨i32⟩(&mut v); |
| 404 | assert vec::len⟨i32⟩(&v) == 0; |
| 405 | } |
| 406 | |
| 407 | @test fn testVecRecordElements() throws (testing::TestError) { |
| 408 | let mut arena: [Obj; 2] = undefined; |
| 409 | let mut v: vec::Vec⟨Obj⟩ = undefined; |
| 410 | vec::init⟨Obj⟩(&mut v, &mut arena[..]); |
| 411 | |
| 412 | assert vec::push⟨Obj⟩(&mut v, Obj { wide: 9, narrow: 1 }); |
| 413 | assert vec::put⟨Obj⟩(&mut v, 0, Obj { wide: 12, narrow: 2 }); |
| 414 | let pair = vec::get⟨Obj⟩(&v, 0) |
| 415 | else throw testing::TestError::Failed; |
| 416 | assert pair.wide == 12; |
| 417 | assert pair.narrow == 2; |
| 418 | } |