Fork of daniellemaywood.uk/gleam — Wasm codegen work
1

Configure Feed

Select the types of activity you want to include in your feed.

More documentation, move tests to own file

+416 -403
+38 -403
erlang-term-format/src/lib.rs
··· 1 1 // SPDX-License-Identifier: Apache-2.0 2 2 // SPDX-FileCopyrightText: 2026 The Gleam contributors 3 3 4 + #[cfg(test)] 5 + mod tests; 6 + 4 7 use num_bigint::{BigInt, Sign}; 5 8 use num_traits::ToPrimitive; 6 9 ··· 10 13 11 14 #[must_use] 12 15 #[derive(Debug)] 13 - pub struct List { 16 + pub struct ListEnder { 17 + /// This is the index in the term builder buffer to where the number of 18 + /// elements in the list is written. When we start a list the number is 19 + /// zeroed as we may not know the number of elements yet, and it is 20 + /// written when finishing the list using this index. 14 21 size_index: usize, 22 + 23 + /// This field is used by the debug mode `Drop` implementation, to ensure 24 + /// that lists are correctly closed after being consumed. 15 25 #[cfg(debug_assertions)] 16 26 used: bool, 17 27 } 18 28 19 - impl List { 29 + impl ListEnder { 20 30 pub fn new(size_index: usize) -> Self { 21 31 Self { 22 32 size_index, ··· 27 37 } 28 38 } 29 39 30 - #[cfg(debug_assertions)] 31 - impl List { 40 + impl ListEnder { 41 + #[cfg(debug_assertions)] 32 42 pub fn consume(mut self) { 33 43 self.used = true; 34 44 } 35 - } 36 45 37 - #[cfg(not(debug_assertions))] 38 - impl List { 46 + #[cfg(not(debug_assertions))] 39 47 pub fn consume(self) {} 40 48 } 41 49 42 50 /// When testing we want to make sure that all lists are always correctly closed 43 - /// after being consumed. In production builds all of these operations become 44 - /// no-ops. 51 + /// after being consumed. In production builds this is not implemented, so there 52 + /// is no runtime cost. 45 53 #[cfg(debug_assertions)] 46 - impl Drop for List { 54 + impl Drop for ListEnder { 47 55 fn drop(&mut self) { 48 56 assert!(self.used, "list not closed"); 49 57 } 50 58 } 51 59 60 + /// The first byte of ETF data is this version number. 61 + /// 62 + const ERLANG_TERM_FORMAT_VERSION_NUMBER: u8 = 131; 63 + 52 64 /// A data structure used to encode values into the Erlang Term Format: 53 65 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html. 54 66 /// 55 67 #[derive(Debug)] 56 - pub struct Builder { 68 + pub struct TermBuilder { 57 69 bytes: Vec<u8>, 58 70 } 59 71 60 - impl Default for Builder { 72 + impl Default for TermBuilder { 61 73 fn default() -> Self { 62 74 Self::new() 63 75 } 64 76 } 65 77 66 - impl Builder { 78 + impl TermBuilder { 67 79 pub fn new() -> Self { 68 - Self { bytes: vec![131] } 80 + Self { 81 + bytes: vec![ERLANG_TERM_FORMAT_VERSION_NUMBER], 82 + } 69 83 } 70 84 71 85 /// Get the binary representation of the data structure built so far. ··· 103 117 /// ``` 104 118 /// 105 119 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#list_ext 106 - pub fn start_list(&mut self) -> List { 120 + pub fn start_list(&mut self) -> ListEnder { 107 121 self.push(108); 108 122 let size_index = self.bytes.len(); 123 + 124 + // These 4 bytes store the number of elements (not including the tail). 125 + // They are set to zero here, and the `end_list` function overwrites them 126 + // with the actual length, as that number may not be known in advance. 109 127 self.push(0); 110 128 self.push(0); 111 129 self.push(0); 112 130 self.push(0); 113 - List::new(size_index) 131 + 132 + ListEnder::new(size_index) 114 133 } 115 134 116 - pub fn end_list(&mut self, list: List, items: u32) { 135 + pub fn end_list(&mut self, list: ListEnder, number_of_element: u32) { 117 136 self.empty_list(); 118 - self.bytes[list.size_index..list.size_index + 4].copy_from_slice(&items.to_be_bytes()); 137 + self.bytes[list.size_index..list.size_index + 4] 138 + .copy_from_slice(&number_of_element.to_be_bytes()); 119 139 list.consume(); 120 140 } 121 141 ··· 233 253 self.extend(name.bytes()); 234 254 } 235 255 } 236 - 237 - #[cfg(test)] 238 - /// We want to test all of these functions against the actual values produced by 239 - /// Erlang when turning some data into a binary. 240 - /// 241 - /// All the bytes you see in the assertions here were produced using the 242 - /// following bit of Erlang: 243 - /// 244 - /// ```erl 245 - /// Binary = term_to_binary(SomeErlangData), 246 - /// io:format("~w~n", [Binary]). 247 - /// ``` 248 - /// 249 - /// This will turn any data structure into its binary representation and print 250 - /// its bytes we can then copy paste in these tests. 251 - /// 252 - /// For example: if I were to test that a list of two atoms is correctly turned 253 - /// into its binary representation I would write the following: 254 - /// 255 - /// ```erl 256 - /// Binary = term_to_binary([nil, ok]), 257 - /// io:format("~w~n", [Binary]). 258 - /// ``` 259 - /// 260 - /// And write a test like this: 261 - /// 262 - /// ```ignore 263 - /// let mut etf = Builder::new(); 264 - /// let list = etf.start_list(); 265 - /// etf.atom("nil"); 266 - /// etf.atom("ok"); 267 - /// etf.end_list(list, 2); 268 - /// 269 - /// assert_eq!( 270 - /// etf.into_vec(), 271 - /// [... the bytes I got from the Erlang code ...] 272 - /// ) 273 - /// ``` 274 - /// 275 - mod tests { 276 - use std::{ops::Neg, str::FromStr}; 277 - 278 - use num_bigint::BigInt; 279 - 280 - use crate::Builder; 281 - 282 - #[test] 283 - fn small_atom() { 284 - let mut etf = Builder::new(); 285 - etf.atom("atom"); 286 - assert_eq!(etf.into_vec(), [131, 119, 4, 97, 116, 111, 109]) 287 - } 288 - 289 - #[test] 290 - fn small_atom_utf8() { 291 - let mut etf = Builder::new(); 292 - etf.atom("ksiąskę"); 293 - assert_eq!( 294 - etf.into_vec(), 295 - [131, 119, 9, 107, 115, 105, 196, 133, 115, 107, 196, 153] 296 - ) 297 - } 298 - 299 - #[test] 300 - fn atom() { 301 - let mut etf = Builder::new(); 302 - etf.atom(&"ą".repeat(128)); 303 - assert_eq!( 304 - etf.into_vec(), 305 - [ 306 - 131, 118, 1, 0, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 307 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 308 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 309 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 310 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 311 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 312 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 313 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 314 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 315 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 316 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 317 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 318 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 319 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 320 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 321 - 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 322 - 133, 196, 133 323 - ] 324 - ) 325 - } 326 - 327 - #[test] 328 - fn small_integer() { 329 - let mut etf = Builder::new(); 330 - etf.small_integer(1); 331 - assert_eq!(etf.into_vec(), [131, 97, 1]); 332 - } 333 - 334 - #[test] 335 - fn integer() { 336 - let mut etf = Builder::new(); 337 - etf.integer(-2); 338 - assert_eq!(etf.into_vec(), [131, 98, 255, 255, 255, 254]); 339 - } 340 - 341 - #[test] 342 - fn integer_2() { 343 - let mut etf = Builder::new(); 344 - etf.integer(1048576); 345 - assert_eq!(etf.into_vec(), [131, 98, 0, 16, 0, 0]); 346 - } 347 - 348 - #[test] 349 - fn float() { 350 - let mut etf = Builder::new(); 351 - etf.new_float(1.2); 352 - assert_eq!(etf.into_vec(), [131, 70, 63, 243, 51, 51, 51, 51, 51, 51]) 353 - } 354 - 355 - #[test] 356 - fn small_big() { 357 - let mut etf = Builder::new(); 358 - 359 - etf.small_big(BigInt::from(123123123123123123 as i64)); 360 - assert_eq!( 361 - etf.into_vec(), 362 - [131, 110, 8, 0, 179, 243, 99, 1, 212, 107, 181, 1] 363 - ); 364 - } 365 - 366 - #[test] 367 - fn negative_small_big() { 368 - let mut etf = Builder::new(); 369 - etf.small_big(BigInt::from(-123123123123123123 as i64)); 370 - assert_eq!( 371 - etf.into_vec(), 372 - [131, 110, 8, 1, 179, 243, 99, 1, 212, 107, 181, 1] 373 - ); 374 - } 375 - 376 - #[test] 377 - fn empty_list() { 378 - let mut etf = Builder::new(); 379 - etf.empty_list(); 380 - assert_eq!(etf.into_vec(), [131, 106]); 381 - } 382 - 383 - #[test] 384 - fn proper_list_with_a_single_item() { 385 - let mut etf = Builder::new(); 386 - let list = etf.start_list(); 387 - etf.small_integer(1); 388 - etf.end_list(list, 1); 389 - assert_eq!(etf.into_vec(), [131, 108, 0, 0, 0, 1, 97, 1, 106]) 390 - } 391 - 392 - #[test] 393 - fn proper_list() { 394 - let mut etf = Builder::new(); 395 - let list = etf.start_list(); 396 - etf.small_integer(1); 397 - etf.small_tuple(0); 398 - etf.small_integer(2); 399 - etf.end_list(list, 3); 400 - assert_eq!( 401 - etf.into_vec(), 402 - [131, 108, 0, 0, 0, 3, 97, 1, 104, 0, 97, 2, 106] 403 - ) 404 - } 405 - 406 - #[test] 407 - fn empty_binary() { 408 - let mut etf = Builder::new(); 409 - etf.binary(0, vec![]); 410 - assert_eq!(etf.into_vec(), [131, 109, 0, 0, 0, 0]) 411 - } 412 - #[test] 413 - fn binary() { 414 - let mut etf = Builder::new(); 415 - etf.binary(3, vec![1, 2, 3]); 416 - assert_eq!(etf.into_vec(), [131, 109, 0, 0, 0, 3, 1, 2, 3]) 417 - } 418 - 419 - #[test] 420 - fn small_empty_tuple() { 421 - let mut etf = Builder::new(); 422 - etf.small_tuple(0); 423 - assert_eq!(etf.into_vec(), [131, 104, 0]) 424 - } 425 - 426 - #[test] 427 - fn small_single_item_tuple() { 428 - let mut etf = Builder::new(); 429 - etf.small_tuple(1); 430 - etf.small_integer(1); 431 - assert_eq!(etf.into_vec(), [131, 104, 1, 97, 1]) 432 - } 433 - 434 - #[test] 435 - fn small_tuple() { 436 - let mut etf = Builder::new(); 437 - etf.small_tuple(3); 438 - etf.new_float(1.1); 439 - etf.integer(-1); 440 - etf.small_integer(11); 441 - 442 - assert_eq!( 443 - etf.into_vec(), 444 - [ 445 - 131, 104, 3, 70, 63, 241, 153, 153, 153, 153, 153, 154, 98, 255, 255, 255, 255, 97, 446 - 11 447 - ] 448 - ) 449 - } 450 - 451 - #[test] 452 - fn small_nested_tuple() { 453 - let mut etf = Builder::new(); 454 - etf.small_tuple(2); 455 - 456 - etf.small_tuple(2); 457 - etf.small_integer(1); 458 - etf.small_integer(11); 459 - 460 - etf.small_tuple(1); 461 - etf.new_float(1.1); 462 - 463 - assert_eq!( 464 - etf.into_vec(), 465 - [ 466 - 131, 104, 2, 104, 2, 97, 1, 97, 11, 104, 1, 70, 63, 241, 153, 153, 153, 153, 153, 467 - 154 468 - ] 469 - ) 470 - } 471 - 472 - #[test] 473 - fn large_tuple() { 474 - let mut etf = Builder::new(); 475 - etf.large_tuple(500); 476 - for _ in 1..=500 { 477 - etf.small_integer(1); 478 - } 479 - 480 - assert_eq!( 481 - etf.into_vec(), 482 - [ 483 - 131, 105, 0, 0, 1, 244, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 484 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 485 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 486 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 487 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 488 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 489 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 490 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 491 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 492 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 493 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 494 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 495 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 496 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 497 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 498 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 499 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 500 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 501 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 502 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 503 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 504 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 505 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 506 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 507 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 508 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 509 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 510 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 511 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 512 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 513 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 514 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 515 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 516 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 517 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 518 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 519 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 520 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 521 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 522 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 523 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 524 - 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1 525 - ] 526 - ) 527 - } 528 - 529 - #[test] 530 - fn large_big() { 531 - let mut etf = Builder::new(); 532 - etf.large_big(BigInt::from_str(&"1".repeat(1500)).unwrap()); 533 - assert_eq!( 534 - etf.into_vec(), 535 - [ 536 - 131, 111, 0, 0, 2, 111, 0, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 537 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 538 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 539 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 540 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 541 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 542 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 543 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 544 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 545 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 546 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 1, 169, 242, 20, 147, 547 - 190, 32, 128, 194, 36, 247, 163, 33, 20, 52, 44, 99, 21, 192, 66, 245, 84, 128, 97, 548 - 156, 59, 26, 49, 215, 19, 60, 165, 65, 99, 28, 91, 91, 132, 187, 191, 48, 27, 217, 549 - 244, 127, 22, 229, 191, 10, 184, 4, 38, 105, 182, 90, 90, 141, 23, 179, 148, 140, 550 - 187, 169, 145, 218, 132, 199, 136, 139, 1, 228, 120, 218, 220, 103, 20, 164, 155, 551 - 60, 1, 158, 242, 71, 1, 60, 69, 6, 13, 244, 52, 42, 222, 129, 25, 127, 251, 133, 552 - 199, 37, 61, 61, 94, 35, 52, 38, 9, 114, 0, 173, 92, 189, 59, 8, 14, 253, 129, 64, 553 - 238, 18, 222, 214, 221, 45, 236, 250, 62, 255, 237, 241, 149, 145, 143, 93, 41, 2, 554 - 156, 28, 29, 102, 103, 8, 227, 146, 128, 117, 183, 231, 113, 20, 148, 190, 253, 95, 555 - 114, 208, 224, 186, 125, 28, 74, 117, 131, 197, 189, 238, 208, 96, 253, 134, 27, 556 - 108, 58, 63, 97, 183, 248, 49, 174, 205, 42, 129, 196, 241, 71, 237, 83, 136, 127, 557 - 61, 225, 107, 199, 146, 30, 212, 53, 215, 141, 147, 198, 3, 37, 170, 175, 29, 43, 558 - 85, 98, 73, 198, 113, 9, 121, 149, 228, 107, 200, 16, 159, 117, 150, 91, 90, 121, 559 - 11, 5, 27, 25, 110, 69, 194, 57, 21, 91, 173, 149, 40, 5, 126, 101, 113, 247, 14, 560 - 130, 154, 249, 36, 143, 183, 129, 4, 174, 108, 164, 122, 95, 105, 77, 243, 58, 158, 561 - 223, 166, 79, 103, 131, 161, 110, 57, 79, 214, 142, 141, 39, 101, 6, 176, 22, 182, 562 - 89, 247, 229, 80, 244, 24, 62, 101, 128, 174, 253, 86, 233, 138, 130, 115, 157, 80, 563 - 21, 83, 187, 222, 43, 19, 105, 97, 230, 7, 167, 25, 6, 69, 207, 149, 32, 199, 44, 564 - 85, 27, 223, 133, 46, 234, 3, 59, 113, 228, 246, 29, 157, 4, 83, 207, 234, 85, 122, 565 - 148, 64, 125, 49, 48, 123, 226, 213, 154, 212, 102, 6, 154, 91, 67, 242, 232, 99, 566 - 154, 238, 73, 72, 122, 170, 27, 110, 83, 110, 146, 159, 87, 87, 163, 151, 196, 47, 567 - 134, 198, 170, 197, 189, 176, 82, 247, 130, 95, 159, 217, 228, 52, 214, 63, 247, 568 - 169, 152, 216, 23, 182, 142, 26, 218, 248, 47, 161, 238, 180, 122, 54, 239, 38, 95, 569 - 65, 114, 128, 93, 233, 71, 177, 101, 2, 236, 244, 88, 249, 104, 210, 4, 249, 5, 570 - 172, 107, 216, 222, 165, 242, 106, 222, 22, 158, 50, 13 571 - ] 572 - ); 573 - } 574 - 575 - #[test] 576 - fn negative_large_big() { 577 - let mut etf = Builder::new(); 578 - etf.large_big(BigInt::from_str(&"1".repeat(1500)).unwrap().neg()); 579 - assert_eq!( 580 - etf.into_vec(), 581 - [ 582 - 131, 111, 0, 0, 2, 111, 1, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 583 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 584 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 585 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 586 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 587 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 588 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 589 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 590 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 591 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 592 - 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 1, 169, 242, 20, 147, 593 - 190, 32, 128, 194, 36, 247, 163, 33, 20, 52, 44, 99, 21, 192, 66, 245, 84, 128, 97, 594 - 156, 59, 26, 49, 215, 19, 60, 165, 65, 99, 28, 91, 91, 132, 187, 191, 48, 27, 217, 595 - 244, 127, 22, 229, 191, 10, 184, 4, 38, 105, 182, 90, 90, 141, 23, 179, 148, 140, 596 - 187, 169, 145, 218, 132, 199, 136, 139, 1, 228, 120, 218, 220, 103, 20, 164, 155, 597 - 60, 1, 158, 242, 71, 1, 60, 69, 6, 13, 244, 52, 42, 222, 129, 25, 127, 251, 133, 598 - 199, 37, 61, 61, 94, 35, 52, 38, 9, 114, 0, 173, 92, 189, 59, 8, 14, 253, 129, 64, 599 - 238, 18, 222, 214, 221, 45, 236, 250, 62, 255, 237, 241, 149, 145, 143, 93, 41, 2, 600 - 156, 28, 29, 102, 103, 8, 227, 146, 128, 117, 183, 231, 113, 20, 148, 190, 253, 95, 601 - 114, 208, 224, 186, 125, 28, 74, 117, 131, 197, 189, 238, 208, 96, 253, 134, 27, 602 - 108, 58, 63, 97, 183, 248, 49, 174, 205, 42, 129, 196, 241, 71, 237, 83, 136, 127, 603 - 61, 225, 107, 199, 146, 30, 212, 53, 215, 141, 147, 198, 3, 37, 170, 175, 29, 43, 604 - 85, 98, 73, 198, 113, 9, 121, 149, 228, 107, 200, 16, 159, 117, 150, 91, 90, 121, 605 - 11, 5, 27, 25, 110, 69, 194, 57, 21, 91, 173, 149, 40, 5, 126, 101, 113, 247, 14, 606 - 130, 154, 249, 36, 143, 183, 129, 4, 174, 108, 164, 122, 95, 105, 77, 243, 58, 158, 607 - 223, 166, 79, 103, 131, 161, 110, 57, 79, 214, 142, 141, 39, 101, 6, 176, 22, 182, 608 - 89, 247, 229, 80, 244, 24, 62, 101, 128, 174, 253, 86, 233, 138, 130, 115, 157, 80, 609 - 21, 83, 187, 222, 43, 19, 105, 97, 230, 7, 167, 25, 6, 69, 207, 149, 32, 199, 44, 610 - 85, 27, 223, 133, 46, 234, 3, 59, 113, 228, 246, 29, 157, 4, 83, 207, 234, 85, 122, 611 - 148, 64, 125, 49, 48, 123, 226, 213, 154, 212, 102, 6, 154, 91, 67, 242, 232, 99, 612 - 154, 238, 73, 72, 122, 170, 27, 110, 83, 110, 146, 159, 87, 87, 163, 151, 196, 47, 613 - 134, 198, 170, 197, 189, 176, 82, 247, 130, 95, 159, 217, 228, 52, 214, 63, 247, 614 - 169, 152, 216, 23, 182, 142, 26, 218, 248, 47, 161, 238, 180, 122, 54, 239, 38, 95, 615 - 65, 114, 128, 93, 233, 71, 177, 101, 2, 236, 244, 88, 249, 104, 210, 4, 249, 5, 616 - 172, 107, 216, 222, 165, 242, 106, 222, 22, 158, 50, 13 617 - ] 618 - ); 619 - } 620 - }
+378
erlang-term-format/src/tests.rs
··· 1 + // SPDX-License-Identifier: Apache-2.0 2 + // SPDX-FileCopyrightText: 2026 The Gleam contributors 3 + 4 + //// We want to test all of these functions against the actual values produced by 5 + //// Erlang when turning some data into a binary. 6 + //// 7 + //// All the bytes you see in the assertions here were produced using the 8 + //// following bit of Erlang: 9 + //// 10 + //// ```erl 11 + //// Binary = term_to_binary(SomeErlangData), 12 + //// io:format("~w~n", [Binary]). 13 + //// ``` 14 + //// 15 + //// This will turn any data structure into its binary representation and print 16 + //// its bytes we can then copy paste in these tests. 17 + //// 18 + //// For example: if I were to test that a list of two atoms is correctly turned 19 + //// into its binary representation I would write the following: 20 + //// 21 + //// ```erl 22 + //// Binary = term_to_binary([nil, ok]), 23 + //// io:format("~w~n", [Binary]). 24 + //// ``` 25 + //// 26 + //// And write a test like this: 27 + //// 28 + //// ```ignore 29 + //// let mut etf = Builder::new(); 30 + //// let list = etf.start_list(); 31 + //// etf.atom("nil"); 32 + //// etf.atom("ok"); 33 + //// etf.end_list(list, 2); 34 + //// 35 + //// assert_eq!( 36 + //// etf.into_vec(), 37 + //// [... the bytes I got from the Erlang code ...] 38 + //// ) 39 + //// ``` 40 + //// 41 + use std::{ops::Neg, str::FromStr}; 42 + 43 + use num_bigint::BigInt; 44 + 45 + use crate::TermBuilder; 46 + 47 + #[test] 48 + fn small_atom() { 49 + let mut etf = TermBuilder::new(); 50 + etf.atom("atom"); 51 + assert_eq!(etf.into_vec(), [131, 119, 4, 97, 116, 111, 109]) 52 + } 53 + 54 + #[test] 55 + fn small_atom_utf8() { 56 + let mut etf = TermBuilder::new(); 57 + etf.atom("ksiąskę"); 58 + assert_eq!( 59 + etf.into_vec(), 60 + [131, 119, 9, 107, 115, 105, 196, 133, 115, 107, 196, 153] 61 + ) 62 + } 63 + 64 + #[test] 65 + fn atom() { 66 + let mut etf = TermBuilder::new(); 67 + etf.atom(&"ą".repeat(128)); 68 + assert_eq!( 69 + etf.into_vec(), 70 + [ 71 + 131, 118, 1, 0, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 72 + 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 73 + 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 74 + 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 75 + 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 76 + 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 77 + 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 78 + 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 79 + 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 80 + 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 81 + 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 82 + 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 83 + 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 84 + 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 85 + 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 86 + 196, 133, 196, 133 87 + ] 88 + ) 89 + } 90 + 91 + #[test] 92 + fn small_integer() { 93 + let mut etf = TermBuilder::new(); 94 + etf.small_integer(1); 95 + assert_eq!(etf.into_vec(), [131, 97, 1]); 96 + } 97 + 98 + #[test] 99 + fn integer() { 100 + let mut etf = TermBuilder::new(); 101 + etf.integer(-2); 102 + assert_eq!(etf.into_vec(), [131, 98, 255, 255, 255, 254]); 103 + } 104 + 105 + #[test] 106 + fn integer_2() { 107 + let mut etf = TermBuilder::new(); 108 + etf.integer(1048576); 109 + assert_eq!(etf.into_vec(), [131, 98, 0, 16, 0, 0]); 110 + } 111 + 112 + #[test] 113 + fn float() { 114 + let mut etf = TermBuilder::new(); 115 + etf.new_float(1.2); 116 + assert_eq!(etf.into_vec(), [131, 70, 63, 243, 51, 51, 51, 51, 51, 51]) 117 + } 118 + 119 + #[test] 120 + fn small_big() { 121 + let mut etf = TermBuilder::new(); 122 + 123 + etf.small_big(BigInt::from(123123123123123123 as i64)); 124 + assert_eq!( 125 + etf.into_vec(), 126 + [131, 110, 8, 0, 179, 243, 99, 1, 212, 107, 181, 1] 127 + ); 128 + } 129 + 130 + #[test] 131 + fn negative_small_big() { 132 + let mut etf = TermBuilder::new(); 133 + etf.small_big(BigInt::from(-123123123123123123 as i64)); 134 + assert_eq!( 135 + etf.into_vec(), 136 + [131, 110, 8, 1, 179, 243, 99, 1, 212, 107, 181, 1] 137 + ); 138 + } 139 + 140 + #[test] 141 + fn empty_list() { 142 + let mut etf = TermBuilder::new(); 143 + etf.empty_list(); 144 + assert_eq!(etf.into_vec(), [131, 106]); 145 + } 146 + 147 + #[test] 148 + fn proper_list_with_a_single_item() { 149 + let mut etf = TermBuilder::new(); 150 + let list = etf.start_list(); 151 + etf.small_integer(1); 152 + etf.end_list(list, 1); 153 + assert_eq!(etf.into_vec(), [131, 108, 0, 0, 0, 1, 97, 1, 106]) 154 + } 155 + 156 + #[test] 157 + fn proper_list() { 158 + let mut etf = TermBuilder::new(); 159 + let list = etf.start_list(); 160 + etf.small_integer(1); 161 + etf.small_tuple(0); 162 + etf.small_integer(2); 163 + etf.end_list(list, 3); 164 + assert_eq!( 165 + etf.into_vec(), 166 + [131, 108, 0, 0, 0, 3, 97, 1, 104, 0, 97, 2, 106] 167 + ) 168 + } 169 + 170 + #[test] 171 + fn empty_binary() { 172 + let mut etf = TermBuilder::new(); 173 + etf.binary(0, vec![]); 174 + assert_eq!(etf.into_vec(), [131, 109, 0, 0, 0, 0]) 175 + } 176 + #[test] 177 + fn binary() { 178 + let mut etf = TermBuilder::new(); 179 + etf.binary(3, vec![1, 2, 3]); 180 + assert_eq!(etf.into_vec(), [131, 109, 0, 0, 0, 3, 1, 2, 3]) 181 + } 182 + 183 + #[test] 184 + fn small_empty_tuple() { 185 + let mut etf = TermBuilder::new(); 186 + etf.small_tuple(0); 187 + assert_eq!(etf.into_vec(), [131, 104, 0]) 188 + } 189 + 190 + #[test] 191 + fn small_single_item_tuple() { 192 + let mut etf = TermBuilder::new(); 193 + etf.small_tuple(1); 194 + etf.small_integer(1); 195 + assert_eq!(etf.into_vec(), [131, 104, 1, 97, 1]) 196 + } 197 + 198 + #[test] 199 + fn small_tuple() { 200 + let mut etf = TermBuilder::new(); 201 + etf.small_tuple(3); 202 + etf.new_float(1.1); 203 + etf.integer(-1); 204 + etf.small_integer(11); 205 + 206 + assert_eq!( 207 + etf.into_vec(), 208 + [ 209 + 131, 104, 3, 70, 63, 241, 153, 153, 153, 153, 153, 154, 98, 255, 255, 255, 255, 97, 11 210 + ] 211 + ) 212 + } 213 + 214 + #[test] 215 + fn small_nested_tuple() { 216 + let mut etf = TermBuilder::new(); 217 + etf.small_tuple(2); 218 + 219 + etf.small_tuple(2); 220 + etf.small_integer(1); 221 + etf.small_integer(11); 222 + 223 + etf.small_tuple(1); 224 + etf.new_float(1.1); 225 + 226 + assert_eq!( 227 + etf.into_vec(), 228 + [ 229 + 131, 104, 2, 104, 2, 97, 1, 97, 11, 104, 1, 70, 63, 241, 153, 153, 153, 153, 153, 154 230 + ] 231 + ) 232 + } 233 + 234 + #[test] 235 + fn large_tuple() { 236 + let mut etf = TermBuilder::new(); 237 + etf.large_tuple(500); 238 + for _ in 1..=500 { 239 + etf.small_integer(1); 240 + } 241 + 242 + assert_eq!( 243 + etf.into_vec(), 244 + [ 245 + 131, 105, 0, 0, 1, 244, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 246 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 247 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 248 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 249 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 250 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 251 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 252 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 253 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 254 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 255 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 256 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 257 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 258 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 259 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 260 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 261 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 262 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 263 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 264 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 265 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 266 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 267 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 268 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 269 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 270 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 271 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 272 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 273 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 274 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 275 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 276 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 277 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 278 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 279 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 280 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 281 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 282 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 283 + 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 284 + 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 285 + 1, 97, 1, 97, 1, 97, 1 286 + ] 287 + ) 288 + } 289 + 290 + #[test] 291 + fn large_big() { 292 + let mut etf = TermBuilder::new(); 293 + etf.large_big(BigInt::from_str(&"1".repeat(1500)).unwrap()); 294 + assert_eq!( 295 + etf.into_vec(), 296 + [ 297 + 131, 111, 0, 0, 2, 111, 0, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 298 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 299 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 300 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 301 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 302 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 303 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 304 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 305 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 306 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 307 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 1, 169, 242, 20, 147, 190, 32, 308 + 128, 194, 36, 247, 163, 33, 20, 52, 44, 99, 21, 192, 66, 245, 84, 128, 97, 156, 59, 26, 309 + 49, 215, 19, 60, 165, 65, 99, 28, 91, 91, 132, 187, 191, 48, 27, 217, 244, 127, 22, 310 + 229, 191, 10, 184, 4, 38, 105, 182, 90, 90, 141, 23, 179, 148, 140, 187, 169, 145, 218, 311 + 132, 199, 136, 139, 1, 228, 120, 218, 220, 103, 20, 164, 155, 60, 1, 158, 242, 71, 1, 312 + 60, 69, 6, 13, 244, 52, 42, 222, 129, 25, 127, 251, 133, 199, 37, 61, 61, 94, 35, 52, 313 + 38, 9, 114, 0, 173, 92, 189, 59, 8, 14, 253, 129, 64, 238, 18, 222, 214, 221, 45, 236, 314 + 250, 62, 255, 237, 241, 149, 145, 143, 93, 41, 2, 156, 28, 29, 102, 103, 8, 227, 146, 315 + 128, 117, 183, 231, 113, 20, 148, 190, 253, 95, 114, 208, 224, 186, 125, 28, 74, 117, 316 + 131, 197, 189, 238, 208, 96, 253, 134, 27, 108, 58, 63, 97, 183, 248, 49, 174, 205, 42, 317 + 129, 196, 241, 71, 237, 83, 136, 127, 61, 225, 107, 199, 146, 30, 212, 53, 215, 141, 318 + 147, 198, 3, 37, 170, 175, 29, 43, 85, 98, 73, 198, 113, 9, 121, 149, 228, 107, 200, 319 + 16, 159, 117, 150, 91, 90, 121, 11, 5, 27, 25, 110, 69, 194, 57, 21, 91, 173, 149, 40, 320 + 5, 126, 101, 113, 247, 14, 130, 154, 249, 36, 143, 183, 129, 4, 174, 108, 164, 122, 95, 321 + 105, 77, 243, 58, 158, 223, 166, 79, 103, 131, 161, 110, 57, 79, 214, 142, 141, 39, 322 + 101, 6, 176, 22, 182, 89, 247, 229, 80, 244, 24, 62, 101, 128, 174, 253, 86, 233, 138, 323 + 130, 115, 157, 80, 21, 83, 187, 222, 43, 19, 105, 97, 230, 7, 167, 25, 6, 69, 207, 149, 324 + 32, 199, 44, 85, 27, 223, 133, 46, 234, 3, 59, 113, 228, 246, 29, 157, 4, 83, 207, 234, 325 + 85, 122, 148, 64, 125, 49, 48, 123, 226, 213, 154, 212, 102, 6, 154, 91, 67, 242, 232, 326 + 99, 154, 238, 73, 72, 122, 170, 27, 110, 83, 110, 146, 159, 87, 87, 163, 151, 196, 47, 327 + 134, 198, 170, 197, 189, 176, 82, 247, 130, 95, 159, 217, 228, 52, 214, 63, 247, 169, 328 + 152, 216, 23, 182, 142, 26, 218, 248, 47, 161, 238, 180, 122, 54, 239, 38, 95, 65, 114, 329 + 128, 93, 233, 71, 177, 101, 2, 236, 244, 88, 249, 104, 210, 4, 249, 5, 172, 107, 216, 330 + 222, 165, 242, 106, 222, 22, 158, 50, 13 331 + ] 332 + ); 333 + } 334 + 335 + #[test] 336 + fn negative_large_big() { 337 + let mut etf = TermBuilder::new(); 338 + etf.large_big(BigInt::from_str(&"1".repeat(1500)).unwrap().neg()); 339 + assert_eq!( 340 + etf.into_vec(), 341 + [ 342 + 131, 111, 0, 0, 2, 111, 1, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 343 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 344 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 345 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 346 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 347 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 348 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 349 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 350 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 351 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 352 + 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 1, 169, 242, 20, 147, 190, 32, 353 + 128, 194, 36, 247, 163, 33, 20, 52, 44, 99, 21, 192, 66, 245, 84, 128, 97, 156, 59, 26, 354 + 49, 215, 19, 60, 165, 65, 99, 28, 91, 91, 132, 187, 191, 48, 27, 217, 244, 127, 22, 355 + 229, 191, 10, 184, 4, 38, 105, 182, 90, 90, 141, 23, 179, 148, 140, 187, 169, 145, 218, 356 + 132, 199, 136, 139, 1, 228, 120, 218, 220, 103, 20, 164, 155, 60, 1, 158, 242, 71, 1, 357 + 60, 69, 6, 13, 244, 52, 42, 222, 129, 25, 127, 251, 133, 199, 37, 61, 61, 94, 35, 52, 358 + 38, 9, 114, 0, 173, 92, 189, 59, 8, 14, 253, 129, 64, 238, 18, 222, 214, 221, 45, 236, 359 + 250, 62, 255, 237, 241, 149, 145, 143, 93, 41, 2, 156, 28, 29, 102, 103, 8, 227, 146, 360 + 128, 117, 183, 231, 113, 20, 148, 190, 253, 95, 114, 208, 224, 186, 125, 28, 74, 117, 361 + 131, 197, 189, 238, 208, 96, 253, 134, 27, 108, 58, 63, 97, 183, 248, 49, 174, 205, 42, 362 + 129, 196, 241, 71, 237, 83, 136, 127, 61, 225, 107, 199, 146, 30, 212, 53, 215, 141, 363 + 147, 198, 3, 37, 170, 175, 29, 43, 85, 98, 73, 198, 113, 9, 121, 149, 228, 107, 200, 364 + 16, 159, 117, 150, 91, 90, 121, 11, 5, 27, 25, 110, 69, 194, 57, 21, 91, 173, 149, 40, 365 + 5, 126, 101, 113, 247, 14, 130, 154, 249, 36, 143, 183, 129, 4, 174, 108, 164, 122, 95, 366 + 105, 77, 243, 58, 158, 223, 166, 79, 103, 131, 161, 110, 57, 79, 214, 142, 141, 39, 367 + 101, 6, 176, 22, 182, 89, 247, 229, 80, 244, 24, 62, 101, 128, 174, 253, 86, 233, 138, 368 + 130, 115, 157, 80, 21, 83, 187, 222, 43, 19, 105, 97, 230, 7, 167, 25, 6, 69, 207, 149, 369 + 32, 199, 44, 85, 27, 223, 133, 46, 234, 3, 59, 113, 228, 246, 29, 157, 4, 83, 207, 234, 370 + 85, 122, 148, 64, 125, 49, 48, 123, 226, 213, 154, 212, 102, 6, 154, 91, 67, 242, 232, 371 + 99, 154, 238, 73, 72, 122, 170, 27, 110, 83, 110, 146, 159, 87, 87, 163, 151, 196, 47, 372 + 134, 198, 170, 197, 189, 176, 82, 247, 130, 95, 159, 217, 228, 52, 214, 63, 247, 169, 373 + 152, 216, 23, 182, 142, 26, 218, 248, 47, 161, 238, 180, 122, 54, 239, 38, 95, 65, 114, 374 + 128, 93, 233, 71, 177, 101, 2, 236, 244, 88, 249, 104, 210, 4, 249, 5, 172, 107, 216, 375 + 222, 165, 242, 106, 222, 22, 158, 50, 13 376 + ] 377 + ); 378 + }