standard_card/card/
create.rs

1/// Create the card from given rank and suit
2///
3/// # Arguments
4/// * `rank` - Rank of the card. Valid value \[0, 12\]
5/// * `suit` - Suit of the card. Valid value \[0, 3\]
6///
7/// # Returns
8/// Card number if the arguments are valid, otherwise 0
9///
10/// # Example
11/// ```
12/// use standard_card::card::create;
13///
14/// let card = create(3, 1);
15/// assert_eq!(541511, card);
16/// ```
17pub fn create(rank: i32, suit: i32) -> i32 {
18    if let 0..=12 = rank {
19        if let 0..=3 = suit {
20            let primes: [i32; 13] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41];
21            let prime: i32 = primes[rank as usize];
22
23            return prime | (suit << 6) | (rank << 8) | (1 << (15 - suit)) | (1 << (16 + rank));
24        }
25    }
26
27    return 0;
28}
29
30#[cfg(test)]
31mod create {
32    use crate::card::create;
33
34    #[test]
35    fn ace_of_clubs() {
36        // Act
37        let actual = create(12, 0);
38
39        // Assert
40        assert_eq!(actual, 268471337);
41    }
42
43    #[test]
44    fn ace_of_diamonds() {
45        // Act
46        let actual = create(12, 1);
47
48        // Assert
49        assert_eq!(actual, 268455017);
50    }
51
52    #[test]
53    fn ace_of_hearts() {
54        // Act
55        let actual = create(12, 2);
56
57        // Assert
58        assert_eq!(actual, 268446889);
59    }
60
61    #[test]
62    fn ace_of_spades() {
63        // Act
64        let actual = create(12, 3);
65
66        // Assert
67        assert_eq!(actual, 268442857);
68    }
69}