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

Configure Feed

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

1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2026 The Gleam contributors 3 4use num_bigint::{BigInt, Sign}; 5use num_traits::ToPrimitive; 6 7#[cfg(test)] 8#[macro_use] 9extern crate pretty_assertions; 10 11#[must_use] 12#[derive(Debug)] 13pub struct List { 14 size_index: usize, 15 #[cfg(debug_assertions)] 16 used: bool, 17} 18 19impl List { 20 pub fn new(size_index: usize) -> Self { 21 Self { 22 size_index, 23 24 #[cfg(debug_assertions)] 25 used: false, 26 } 27 } 28} 29 30#[cfg(debug_assertions)] 31impl List { 32 pub fn consume(mut self) { 33 self.used = true; 34 } 35} 36 37#[cfg(not(debug_assertions))] 38impl List { 39 pub fn consume(self) {} 40} 41 42/// 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. 45#[cfg(debug_assertions)] 46impl Drop for List { 47 fn drop(&mut self) { 48 assert!(self.used, "list not closed"); 49 } 50} 51 52/// A data structure used to encode values into the Erlang Term Format: 53/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html. 54/// 55#[derive(Debug)] 56pub struct Builder { 57 bytes: Vec<u8>, 58} 59 60impl Default for Builder { 61 fn default() -> Self { 62 Self::new() 63 } 64} 65 66impl Builder { 67 pub fn new() -> Self { 68 Self { bytes: vec![131] } 69 } 70 71 /// Get the binary representation of the data structure built so far. 72 pub fn into_vec(self) -> Vec<u8> { 73 self.bytes 74 } 75 76 fn push(&mut self, byte: u8) { 77 self.bytes.push(byte); 78 } 79 80 fn extend(&mut self, bytes: impl IntoIterator<Item = u8>) { 81 self.bytes.extend(bytes); 82 } 83 84 /// Pushes a single raw byte. 85 /// 86 pub fn raw_byte(&mut self, byte: u8) { 87 self.push(byte); 88 } 89 90 /// Start building a list with a number of item that is not known in 91 /// advance. 92 /// 93 /// Once you've then pushed all the items, you _must_ complete the list by 94 /// calling `end_list` with the number of items that were pushed. 95 /// 96 /// ```ignore 97 /// // [1, 2, 3] 98 /// let list = etf.start_list() 99 /// etf.small_integer(1); 100 /// etf.small_integer(2); 101 /// etf.small_integer(3); 102 /// etf.end_list(list, 3) 103 /// ``` 104 /// 105 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#list_ext 106 pub fn start_list(&mut self) -> List { 107 self.push(108); 108 let size_index = self.bytes.len(); 109 self.push(0); 110 self.push(0); 111 self.push(0); 112 self.push(0); 113 List::new(size_index) 114 } 115 116 pub fn end_list(&mut self, list: List, items: u32) { 117 self.empty_list(); 118 self.bytes[list.size_index..list.size_index + 4].copy_from_slice(&items.to_be_bytes()); 119 list.consume(); 120 } 121 122 /// Pushes the most compact representation of the given atom. 123 pub fn atom(&mut self, atom: &str) { 124 // TODO: we could explore using ATOM_CACHE_REF in future. 125 // Might be able to get slightly faster ETF parsing out of erlc if we 126 // use it: 127 // https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#atom_cache_ref 128 if atom.len() <= 255 { 129 self.small_atom_utf8(atom); 130 } else { 131 self.atom_utf8(atom); 132 } 133 } 134 135 /// Pushes the most compact representation of the given big int. 136 pub fn bigint(&mut self, value: BigInt) { 137 if let Some(value) = value.to_u8() { 138 self.small_integer(value); 139 } else if let Some(value) = value.to_i32() { 140 self.integer(value); 141 } else { 142 self.small_big(value); 143 } 144 } 145 146 /// Pushes the most compact representation of the given usize int. 147 /// 148 pub fn usize(&mut self, value: usize) { 149 if let Some(value) = value.to_u8() { 150 self.small_integer(value); 151 } else { 152 self.integer(value as i32); 153 } 154 } 155 156 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#small_integer_ext 157 fn small_integer(&mut self, value: u8) { 158 self.push(97); 159 self.push(value); 160 } 161 162 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#integer_ext 163 fn integer(&mut self, value: i32) { 164 self.push(98); 165 self.extend(value.to_be_bytes()); 166 } 167 168 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#new_float_ext 169 pub fn new_float(&mut self, value: f64) { 170 self.push(70); 171 self.extend(value.to_be_bytes()); 172 } 173 174 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#small_tuple_ext 175 pub fn small_tuple(&mut self, arity: u8) { 176 self.push(104); 177 self.push(arity); 178 } 179 180 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#large_tuple_ext 181 pub fn large_tuple(&mut self, arity: u32) { 182 self.push(105); 183 self.extend(arity.to_be_bytes()); 184 } 185 186 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#nil_ext 187 pub fn empty_list(&mut self) { 188 self.push(106); 189 } 190 191 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#binary_ext 192 pub fn binary(&mut self, bytes_count: u32, bytes: impl IntoIterator<Item = u8>) { 193 self.push(109); 194 self.extend(bytes_count.to_be_bytes()); 195 self.extend(bytes); 196 } 197 198 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#small_big_ext 199 fn small_big(&mut self, number: BigInt) { 200 let (sign, bytes) = number.to_bytes_le(); 201 self.push(110); 202 self.push(bytes.len() as u8); 203 match sign { 204 Sign::NoSign | Sign::Plus => self.push(0), 205 Sign::Minus => self.push(1), 206 } 207 self.extend(bytes); 208 } 209 210 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#large_big_ext 211 pub fn large_big(&mut self, number: BigInt) { 212 let (sign, bytes) = number.to_bytes_le(); 213 self.push(111); 214 self.extend((bytes.len() as u32).to_be_bytes()); 215 match sign { 216 Sign::NoSign | Sign::Plus => self.push(0), 217 Sign::Minus => self.push(1), 218 } 219 self.extend(bytes); 220 } 221 222 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#small_atom_utf8_ext 223 fn small_atom_utf8(&mut self, name: &str) { 224 self.push(119); 225 self.push(name.len() as u8); 226 self.extend(name.bytes()); 227 } 228 229 /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#atom_utf8_ext 230 fn atom_utf8(&mut self, name: &str) { 231 self.push(118); 232 self.extend((name.len() as u16).to_be_bytes()); 233 self.extend(name.bytes()); 234 } 235} 236 237#[cfg(test)] 238mod tests { 239 use std::{ops::Neg, str::FromStr}; 240 241 use num_bigint::BigInt; 242 243 use crate::Builder; 244 245 #[test] 246 fn small_atom() { 247 let mut etf = Builder::new(); 248 etf.atom("atom"); 249 assert_eq!(etf.into_vec(), [131, 119, 4, 97, 116, 111, 109]) 250 } 251 252 #[test] 253 fn small_atom_utf8() { 254 let mut etf = Builder::new(); 255 etf.atom("ksiąskę"); 256 assert_eq!( 257 etf.into_vec(), 258 [131, 119, 9, 107, 115, 105, 196, 133, 115, 107, 196, 153] 259 ) 260 } 261 262 #[test] 263 fn atom() { 264 let mut etf = Builder::new(); 265 etf.atom(&"ą".repeat(128)); 266 assert_eq!( 267 etf.into_vec(), 268 [ 269 131, 118, 1, 0, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 270 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 271 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 272 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 273 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 274 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 275 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 276 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 277 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 278 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 279 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 280 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 281 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 282 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 283 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 284 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 285 133, 196, 133 286 ] 287 ) 288 } 289 290 #[test] 291 fn small_integer() { 292 let mut etf = Builder::new(); 293 etf.small_integer(1); 294 assert_eq!(etf.into_vec(), [131, 97, 1]); 295 } 296 297 #[test] 298 fn integer() { 299 let mut etf = Builder::new(); 300 etf.integer(-2); 301 assert_eq!(etf.into_vec(), [131, 98, 255, 255, 255, 254]); 302 } 303 304 #[test] 305 fn integer_2() { 306 let mut etf = Builder::new(); 307 etf.integer(1048576); 308 assert_eq!(etf.into_vec(), [131, 98, 0, 16, 0, 0]); 309 } 310 311 #[test] 312 fn float() { 313 let mut etf = Builder::new(); 314 etf.new_float(1.2); 315 assert_eq!(etf.into_vec(), [131, 70, 63, 243, 51, 51, 51, 51, 51, 51]) 316 } 317 318 #[test] 319 fn small_big() { 320 let mut etf = Builder::new(); 321 322 etf.small_big(BigInt::from(123123123123123123 as i64)); 323 assert_eq!( 324 etf.into_vec(), 325 [131, 110, 8, 0, 179, 243, 99, 1, 212, 107, 181, 1] 326 ); 327 } 328 329 #[test] 330 fn negative_small_big() { 331 let mut etf = Builder::new(); 332 etf.small_big(BigInt::from(-123123123123123123 as i64)); 333 assert_eq!( 334 etf.into_vec(), 335 [131, 110, 8, 1, 179, 243, 99, 1, 212, 107, 181, 1] 336 ); 337 } 338 339 #[test] 340 fn empty_list() { 341 let mut etf = Builder::new(); 342 etf.empty_list(); 343 assert_eq!(etf.into_vec(), [131, 106]); 344 } 345 346 #[test] 347 fn proper_list_with_a_single_item() { 348 let mut etf = Builder::new(); 349 let list = etf.start_list(); 350 etf.small_integer(1); 351 etf.end_list(list, 1); 352 assert_eq!(etf.into_vec(), [131, 108, 0, 0, 0, 1, 97, 1, 106]) 353 } 354 355 #[test] 356 fn proper_list() { 357 let mut etf = Builder::new(); 358 let list = etf.start_list(); 359 etf.small_integer(1); 360 etf.small_tuple(0); 361 etf.small_integer(2); 362 etf.end_list(list, 3); 363 assert_eq!( 364 etf.into_vec(), 365 [131, 108, 0, 0, 0, 3, 97, 1, 104, 0, 97, 2, 106] 366 ) 367 } 368 369 #[test] 370 fn empty_binary() { 371 let mut etf = Builder::new(); 372 etf.binary(0, vec![]); 373 assert_eq!(etf.into_vec(), [131, 109, 0, 0, 0, 0]) 374 } 375 #[test] 376 fn binary() { 377 let mut etf = Builder::new(); 378 etf.binary(3, vec![1, 2, 3]); 379 assert_eq!(etf.into_vec(), [131, 109, 0, 0, 0, 3, 1, 2, 3]) 380 } 381 382 #[test] 383 fn small_empty_tuple() { 384 let mut etf = Builder::new(); 385 etf.small_tuple(0); 386 assert_eq!(etf.into_vec(), [131, 104, 0]) 387 } 388 389 #[test] 390 fn small_single_item_tuple() { 391 let mut etf = Builder::new(); 392 etf.small_tuple(1); 393 etf.small_integer(1); 394 assert_eq!(etf.into_vec(), [131, 104, 1, 97, 1]) 395 } 396 397 #[test] 398 fn small_tuple() { 399 let mut etf = Builder::new(); 400 etf.small_tuple(3); 401 etf.new_float(1.1); 402 etf.integer(-1); 403 etf.small_integer(11); 404 405 assert_eq!( 406 etf.into_vec(), 407 [ 408 131, 104, 3, 70, 63, 241, 153, 153, 153, 153, 153, 154, 98, 255, 255, 255, 255, 97, 409 11 410 ] 411 ) 412 } 413 414 #[test] 415 fn small_nested_tuple() { 416 let mut etf = Builder::new(); 417 etf.small_tuple(2); 418 419 etf.small_tuple(2); 420 etf.small_integer(1); 421 etf.small_integer(11); 422 423 etf.small_tuple(1); 424 etf.new_float(1.1); 425 426 assert_eq!( 427 etf.into_vec(), 428 [ 429 131, 104, 2, 104, 2, 97, 1, 97, 11, 104, 1, 70, 63, 241, 153, 153, 153, 153, 153, 430 154 431 ] 432 ) 433 } 434 435 #[test] 436 fn large_tuple() { 437 let mut etf = Builder::new(); 438 etf.large_tuple(500); 439 for _ in 1..=500 { 440 etf.small_integer(1); 441 } 442 443 assert_eq!( 444 etf.into_vec(), 445 [ 446 131, 105, 0, 0, 1, 244, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 447 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 448 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 449 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 450 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 451 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 452 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 453 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 454 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 455 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 456 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 457 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 458 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 459 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 460 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 461 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 462 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 463 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 464 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 465 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 466 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 467 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 468 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 469 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 470 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 471 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 472 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 473 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 474 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 475 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 476 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 477 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 478 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 479 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 480 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 481 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 482 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 483 1, 97, 1, 97, 1, 97, 1, 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 488 ] 489 ) 490 } 491 492 #[test] 493 fn large_big() { 494 let mut etf = Builder::new(); 495 etf.large_big(BigInt::from_str(&"1".repeat(1500)).unwrap()); 496 assert_eq!( 497 etf.into_vec(), 498 [ 499 131, 111, 0, 0, 2, 111, 0, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 500 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 501 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 502 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 503 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 504 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 505 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 506 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 507 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 508 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 509 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 1, 169, 242, 20, 147, 510 190, 32, 128, 194, 36, 247, 163, 33, 20, 52, 44, 99, 21, 192, 66, 245, 84, 128, 97, 511 156, 59, 26, 49, 215, 19, 60, 165, 65, 99, 28, 91, 91, 132, 187, 191, 48, 27, 217, 512 244, 127, 22, 229, 191, 10, 184, 4, 38, 105, 182, 90, 90, 141, 23, 179, 148, 140, 513 187, 169, 145, 218, 132, 199, 136, 139, 1, 228, 120, 218, 220, 103, 20, 164, 155, 514 60, 1, 158, 242, 71, 1, 60, 69, 6, 13, 244, 52, 42, 222, 129, 25, 127, 251, 133, 515 199, 37, 61, 61, 94, 35, 52, 38, 9, 114, 0, 173, 92, 189, 59, 8, 14, 253, 129, 64, 516 238, 18, 222, 214, 221, 45, 236, 250, 62, 255, 237, 241, 149, 145, 143, 93, 41, 2, 517 156, 28, 29, 102, 103, 8, 227, 146, 128, 117, 183, 231, 113, 20, 148, 190, 253, 95, 518 114, 208, 224, 186, 125, 28, 74, 117, 131, 197, 189, 238, 208, 96, 253, 134, 27, 519 108, 58, 63, 97, 183, 248, 49, 174, 205, 42, 129, 196, 241, 71, 237, 83, 136, 127, 520 61, 225, 107, 199, 146, 30, 212, 53, 215, 141, 147, 198, 3, 37, 170, 175, 29, 43, 521 85, 98, 73, 198, 113, 9, 121, 149, 228, 107, 200, 16, 159, 117, 150, 91, 90, 121, 522 11, 5, 27, 25, 110, 69, 194, 57, 21, 91, 173, 149, 40, 5, 126, 101, 113, 247, 14, 523 130, 154, 249, 36, 143, 183, 129, 4, 174, 108, 164, 122, 95, 105, 77, 243, 58, 158, 524 223, 166, 79, 103, 131, 161, 110, 57, 79, 214, 142, 141, 39, 101, 6, 176, 22, 182, 525 89, 247, 229, 80, 244, 24, 62, 101, 128, 174, 253, 86, 233, 138, 130, 115, 157, 80, 526 21, 83, 187, 222, 43, 19, 105, 97, 230, 7, 167, 25, 6, 69, 207, 149, 32, 199, 44, 527 85, 27, 223, 133, 46, 234, 3, 59, 113, 228, 246, 29, 157, 4, 83, 207, 234, 85, 122, 528 148, 64, 125, 49, 48, 123, 226, 213, 154, 212, 102, 6, 154, 91, 67, 242, 232, 99, 529 154, 238, 73, 72, 122, 170, 27, 110, 83, 110, 146, 159, 87, 87, 163, 151, 196, 47, 530 134, 198, 170, 197, 189, 176, 82, 247, 130, 95, 159, 217, 228, 52, 214, 63, 247, 531 169, 152, 216, 23, 182, 142, 26, 218, 248, 47, 161, 238, 180, 122, 54, 239, 38, 95, 532 65, 114, 128, 93, 233, 71, 177, 101, 2, 236, 244, 88, 249, 104, 210, 4, 249, 5, 533 172, 107, 216, 222, 165, 242, 106, 222, 22, 158, 50, 13 534 ] 535 ); 536 } 537 538 #[test] 539 fn negative_large_big() { 540 let mut etf = Builder::new(); 541 etf.large_big(BigInt::from_str(&"1".repeat(1500)).unwrap().neg()); 542 assert_eq!( 543 etf.into_vec(), 544 [ 545 131, 111, 0, 0, 2, 111, 1, 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, 113, 28, 199, 113, 28, 547 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 548 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 549 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 550 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 551 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 552 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 553 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 554 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 555 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 1, 169, 242, 20, 147, 556 190, 32, 128, 194, 36, 247, 163, 33, 20, 52, 44, 99, 21, 192, 66, 245, 84, 128, 97, 557 156, 59, 26, 49, 215, 19, 60, 165, 65, 99, 28, 91, 91, 132, 187, 191, 48, 27, 217, 558 244, 127, 22, 229, 191, 10, 184, 4, 38, 105, 182, 90, 90, 141, 23, 179, 148, 140, 559 187, 169, 145, 218, 132, 199, 136, 139, 1, 228, 120, 218, 220, 103, 20, 164, 155, 560 60, 1, 158, 242, 71, 1, 60, 69, 6, 13, 244, 52, 42, 222, 129, 25, 127, 251, 133, 561 199, 37, 61, 61, 94, 35, 52, 38, 9, 114, 0, 173, 92, 189, 59, 8, 14, 253, 129, 64, 562 238, 18, 222, 214, 221, 45, 236, 250, 62, 255, 237, 241, 149, 145, 143, 93, 41, 2, 563 156, 28, 29, 102, 103, 8, 227, 146, 128, 117, 183, 231, 113, 20, 148, 190, 253, 95, 564 114, 208, 224, 186, 125, 28, 74, 117, 131, 197, 189, 238, 208, 96, 253, 134, 27, 565 108, 58, 63, 97, 183, 248, 49, 174, 205, 42, 129, 196, 241, 71, 237, 83, 136, 127, 566 61, 225, 107, 199, 146, 30, 212, 53, 215, 141, 147, 198, 3, 37, 170, 175, 29, 43, 567 85, 98, 73, 198, 113, 9, 121, 149, 228, 107, 200, 16, 159, 117, 150, 91, 90, 121, 568 11, 5, 27, 25, 110, 69, 194, 57, 21, 91, 173, 149, 40, 5, 126, 101, 113, 247, 14, 569 130, 154, 249, 36, 143, 183, 129, 4, 174, 108, 164, 122, 95, 105, 77, 243, 58, 158, 570 223, 166, 79, 103, 131, 161, 110, 57, 79, 214, 142, 141, 39, 101, 6, 176, 22, 182, 571 89, 247, 229, 80, 244, 24, 62, 101, 128, 174, 253, 86, 233, 138, 130, 115, 157, 80, 572 21, 83, 187, 222, 43, 19, 105, 97, 230, 7, 167, 25, 6, 69, 207, 149, 32, 199, 44, 573 85, 27, 223, 133, 46, 234, 3, 59, 113, 228, 246, 29, 157, 4, 83, 207, 234, 85, 122, 574 148, 64, 125, 49, 48, 123, 226, 213, 154, 212, 102, 6, 154, 91, 67, 242, 232, 99, 575 154, 238, 73, 72, 122, 170, 27, 110, 83, 110, 146, 159, 87, 87, 163, 151, 196, 47, 576 134, 198, 170, 197, 189, 176, 82, 247, 130, 95, 159, 217, 228, 52, 214, 63, 247, 577 169, 152, 216, 23, 182, 142, 26, 218, 248, 47, 161, 238, 180, 122, 54, 239, 38, 95, 578 65, 114, 128, 93, 233, 71, 177, 101, 2, 236, 244, 88, 249, 104, 210, 4, 249, 5, 579 172, 107, 216, 222, 165, 242, 106, 222, 22, 158, 50, 13 580 ] 581 ); 582 } 583}