summaryrefslogtreecommitdiffstats
path: root/third_party/rust/num-bigint/tests/serde.rs
blob: 0f3d4868ed5b3c626379b612eecc6bc205788f0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! Test serialization and deserialization of `BigUint` and `BigInt`
//!
//! The serialized formats should not change, even if we change our
//! internal representation, because we want to preserve forward and
//! backward compatibility of serialized data!

#![cfg(feature = "serde")]

extern crate num_bigint;
extern crate num_traits;
extern crate serde_test;

use num_bigint::{BigInt, BigUint};
use num_traits::{One, Zero};
use serde_test::{assert_tokens, Token};

#[test]
fn biguint_zero() {
    let tokens = [Token::Seq { len: Some(0) }, Token::SeqEnd];
    assert_tokens(&BigUint::zero(), &tokens);
}

#[test]
fn bigint_zero() {
    let tokens = [
        Token::Tuple { len: 2 },
        Token::I8(0),
        Token::Seq { len: Some(0) },
        Token::SeqEnd,
        Token::TupleEnd,
    ];
    assert_tokens(&BigInt::zero(), &tokens);
}

#[test]
fn biguint_one() {
    let tokens = [Token::Seq { len: Some(1) }, Token::U32(1), Token::SeqEnd];
    assert_tokens(&BigUint::one(), &tokens);
}

#[test]
fn bigint_one() {
    let tokens = [
        Token::Tuple { len: 2 },
        Token::I8(1),
        Token::Seq { len: Some(1) },
        Token::U32(1),
        Token::SeqEnd,
        Token::TupleEnd,
    ];
    assert_tokens(&BigInt::one(), &tokens);
}

#[test]
fn bigint_negone() {
    let tokens = [
        Token::Tuple { len: 2 },
        Token::I8(-1),
        Token::Seq { len: Some(1) },
        Token::U32(1),
        Token::SeqEnd,
        Token::TupleEnd,
    ];
    assert_tokens(&-BigInt::one(), &tokens);
}

// Generated independently from python `hex(factorial(100))`
const FACTORIAL_100: &'static [u32] = &[
    0x00000000, 0x00000000, 0x00000000, 0x2735c61a, 0xee8b02ea, 0xb3b72ed2, 0x9420c6ec, 0x45570cca,
    0xdf103917, 0x943a321c, 0xeb21b5b2, 0x66ef9a70, 0xa40d16e9, 0x28d54bbd, 0xdc240695, 0x964ec395,
    0x1b30,
];

#[test]
fn biguint_factorial_100() {
    let n: BigUint = (1u8..101).product();

    let mut tokens = vec![];
    tokens.push(Token::Seq {
        len: Some(FACTORIAL_100.len()),
    });
    tokens.extend(FACTORIAL_100.iter().map(|&u| Token::U32(u)));
    tokens.push(Token::SeqEnd);

    assert_tokens(&n, &tokens);
}

#[test]
fn bigint_factorial_100() {
    let n: BigInt = (1i8..101).product();

    let mut tokens = vec![];
    tokens.push(Token::Tuple { len: 2 });
    tokens.push(Token::I8(1));
    tokens.push(Token::Seq {
        len: Some(FACTORIAL_100.len()),
    });
    tokens.extend(FACTORIAL_100.iter().map(|&u| Token::U32(u)));
    tokens.push(Token::SeqEnd);
    tokens.push(Token::TupleEnd);

    assert_tokens(&n, &tokens);
}