il2_test_utils/samples/mod.rs
1/*
2 * BSD 3-Clause License
3 *
4 * Copyright (c) 2019-2020, InterlockLedger Network
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * * Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32//! This module contains functions that can be used to create sample values
33//! to be used in tests.
34use std::ops::AddAssign;
35
36#[cfg(test)]
37mod tests;
38
39/// Fills the mutable slice with the specified value.
40///
41/// Arguments:
42/// - `target`: The slice to be filled;
43/// - `value`: The value;
44pub fn fill_with_value<T: Copy>(target: &mut [T], value: T) {
45 for t in target {
46 *t = value;
47 }
48}
49
50/// Fills the mutable slice with a sequence of values.
51///
52/// Arguments:
53/// - `target`: The slice to be filled;
54/// - `initial`: The initial value;
55/// - `inc`: The increment;
56pub fn fill_with_seq<T: AddAssign + Copy>(target: &mut [T], initial: T, inc: T) {
57 let mut curr = initial;
58 for t in target {
59 *t = curr;
60 curr += inc;
61 }
62}
63
64/// Fills the mutable slice with a sequence of values generated by a generetor
65/// function.
66///
67/// This function receives the current value and must return the next value of the
68/// sequence.
69///
70/// ```
71/// let mut v: [u32; 6] = [0; 6];
72/// // Using a generator based on the Collatz conjecture.
73/// fill_with_seq_gen(&mut v, 5, |v| if v % 2 == 0 { v / 2 } else { 3 * v + 1 });
74/// let exp: [u32; 6] = [5, 16, 8, 4, 2, 1];
75/// assert_eq!(&v, &exp);
76/// ```
77///
78/// Arguments:
79/// - `target`: The slice to be filled;
80/// - `initial`: The initial value;
81/// - `gen`: The function used to generate the next state;
82pub fn fill_with_seq_gen<T: Copy>(target: &mut [T], initial: T, gen: fn(T) -> T) {
83 let mut curr = initial;
84 for t in target {
85 *t = curr;
86 curr = gen(curr);
87 }
88}
89
90/// Fills the mutable slice with a sequence of values generated by a generetor.
91/// This function extracts the next value form the generator.
92///
93/// ```
94/// struct Gen(u32, u32);
95/// let mut v: [u32; 6] = [0; 6];
96/// let mut g: Gen = Gen { 0: 0, 1: 1 };
97///
98/// // Using Fibonacci generator
99/// fill_with_generator(&mut v, &mut g, |g| {
100/// let r = g.0;
101/// g.0 = g.1;
102/// g.1 += r;
103/// r
104/// });
105/// let exp: [u32; 6] = [0, 1, 1, 2, 3, 5];
106/// assert_eq!(&v, &exp);
107/// ```
108///
109/// Arguments:
110/// - `target`: The slice to be filled;
111/// - `generator`: The generator;
112/// - `next`: The function used to extract the next value from the generator;
113pub fn fill_with_generator<T: Copy, G>(target: &mut [T], generator: &mut G, next: fn(&mut G) -> T) {
114 for t in target {
115 *t = next(generator);
116 }
117}