1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
use {
18
    crate::{assignment::Assignment, tests::Test},
19
    sp_runtime::Perbill,
20
    tp_traits::FullRotationMode,
21
};
22

            
23
#[test]
24
1
fn keep_subset_keep_50_percent() {
25
1
    let mut collators = vec![1, 2, 3, 4, 5];
26
1
    let mut shuffle_count = 0;
27
1

            
28
1
    let mut shuffle = |_collators: &mut Vec<u64>| {
29
1
        shuffle_count += 1;
30
1
    };
31

            
32
1
    let full_rotation_mode = FullRotationMode::KeepPerbill {
33
1
        percentage: Perbill::from_percent(50),
34
1
    };
35
1
    let max_collators = 5;
36
1
    Assignment::<Test>::keep_collator_subset(
37
1
        Some(&mut collators),
38
1
        full_rotation_mode,
39
1
        max_collators,
40
1
        Some(&mut shuffle),
41
1
    );
42
1

            
43
1
    // 50% of 5 is 2
44
1
    assert_eq!(collators.len(), 2);
45
1
    assert_eq!(shuffle_count, 1);
46
1
}
47

            
48
#[test]
49
1
fn keep_subset_keep_2_collators() {
50
1
    let mut collators = vec![1, 2, 3, 4, 5];
51
1
    let mut shuffle_count = 0;
52
1

            
53
1
    let mut shuffle = |_collators: &mut Vec<u64>| {
54
1
        shuffle_count += 1;
55
1
    };
56

            
57
1
    let full_rotation_mode = FullRotationMode::KeepCollators { keep: 2 };
58
1
    let max_collators = 5;
59
1
    Assignment::<Test>::keep_collator_subset(
60
1
        Some(&mut collators),
61
1
        full_rotation_mode,
62
1
        max_collators,
63
1
        Some(&mut shuffle),
64
1
    );
65
1

            
66
1
    assert_eq!(collators.len(), 2);
67
1
    assert_eq!(shuffle_count, 1);
68
1
}
69

            
70
#[test]
71
1
fn keep_subset_rotate_all() {
72
1
    let mut collators = vec![1, 2, 3, 4, 5];
73
1
    let mut shuffle_count = 0;
74
1

            
75
1
    let mut shuffle = |_collators: &mut Vec<u64>| {
76
        shuffle_count += 1;
77
    };
78

            
79
1
    let full_rotation_mode = FullRotationMode::RotateAll;
80
1
    let max_collators = 5;
81
1
    Assignment::<Test>::keep_collator_subset(
82
1
        Some(&mut collators),
83
1
        full_rotation_mode,
84
1
        max_collators,
85
1
        Some(&mut shuffle),
86
1
    );
87
1

            
88
1
    assert_eq!(collators.len(), 0);
89
1
    assert_eq!(shuffle_count, 0);
90
1
}
91

            
92
#[test]
93
1
fn keep_subset_keep_all() {
94
1
    let mut collators = vec![1, 2, 3, 4, 5];
95
1
    let mut shuffle_count = 0;
96
1

            
97
1
    let mut shuffle = |_collators: &mut Vec<u64>| {
98
        shuffle_count += 1;
99
    };
100

            
101
1
    let full_rotation_mode = FullRotationMode::KeepAll;
102
1
    let max_collators = 5;
103
1
    Assignment::<Test>::keep_collator_subset(
104
1
        Some(&mut collators),
105
1
        full_rotation_mode,
106
1
        max_collators,
107
1
        Some(&mut shuffle),
108
1
    );
109
1

            
110
1
    assert_eq!(collators.len(), 5);
111
1
    assert_eq!(shuffle_count, 0);
112
1
}
113

            
114
#[test]
115
1
fn keep_subset_empty_collators() {
116
1
    let mut collators = vec![];
117
1
    let mut shuffle_count = 0;
118
1

            
119
1
    let mut shuffle = |_collators: &mut Vec<u64>| {
120
        shuffle_count += 1;
121
    };
122

            
123
1
    let full_rotation_mode = FullRotationMode::KeepCollators { keep: 2 };
124
1
    let max_collators = 5;
125
1
    Assignment::<Test>::keep_collator_subset(
126
1
        Some(&mut collators),
127
1
        full_rotation_mode.clone(),
128
1
        max_collators,
129
1
        Some(&mut shuffle),
130
1
    );
131
1
    assert_eq!(collators.len(), 0);
132

            
133
    // Calling this with None does not panic
134
1
    Assignment::<Test>::keep_collator_subset(
135
1
        None,
136
1
        full_rotation_mode,
137
1
        max_collators,
138
1
        Some(&mut shuffle),
139
1
    );
140
1
    assert_eq!(shuffle_count, 0);
141
1
}
142

            
143
#[test]
144
1
fn keep_subset_keep_more_than_max() {
145
1
    // Trying to keep more collators than the max keeps all of them and does not panic
146
1
    let mut collators = vec![1, 2, 3, 4, 5];
147
1
    let mut shuffle_count = 0;
148
1

            
149
1
    let mut shuffle = |_collators: &mut Vec<u64>| {
150
        shuffle_count += 1;
151
    };
152

            
153
1
    let full_rotation_mode = FullRotationMode::KeepCollators { keep: 200 };
154
1
    let max_collators = 5;
155
1
    Assignment::<Test>::keep_collator_subset(
156
1
        Some(&mut collators),
157
1
        full_rotation_mode.clone(),
158
1
        max_collators,
159
1
        Some(&mut shuffle),
160
1
    );
161
1
    assert_eq!(collators.len(), 5);
162
1
    assert_eq!(shuffle_count, 0);
163
1
}