• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TinTeam / SN-50 / 14149942514

29 Mar 2025 10:20PM UTC coverage: 91.618% (-1.7%) from 93.269%
14149942514

Pull #5

github

web-flow
Merge 11985a483 into d5615e274
Pull Request #5: refactor: improve tinlib code

36 of 62 new or added lines in 4 files covered. (58.06%)

6 existing lines in 2 files now uncovered.

1716 of 1873 relevant lines covered (91.62%)

36926.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/tinlib/src/graphic/palette.rs
1
//! Palette implementation and manipulation.
2
use std::fmt;
3
use std::slice;
4

5
use crate::common::{CommonError, Result};
6
use crate::graphic::color::Color;
7

8
/// Default number of colors in a Palette.
9
const NUM_COLORS_IN_PALETTE: usize = 16;
10

11
/// A iterator over all palette colors.
12
pub type PaletteColorIter<'iter> = slice::Iter<'iter, Color>;
13
/// A mutable iterator over all palette colors.
14
pub type PaletteColorIterMut<'iter> = slice::IterMut<'iter, Color>;
15

16
/// A Palette representation with N colors.
17
#[derive(Clone)]
18
pub struct Palette {
19
    /// Palette's colors.
20
    colors: Vec<Color>,
21
}
22

23
impl Palette {
24
    /// Creates a new Palette.
NEW
25
    pub fn new(num_colors: usize) -> Self {
×
NEW
26
        Self {
×
NEW
27
            colors: vec![Color::default(); num_colors],
×
NEW
28
        }
×
NEW
29
    }
×
30

31
    /// Returns the lenght.
32
    pub fn lenght(&self) -> usize {
10✔
33
        self.colors.len()
10✔
34
    }
10✔
35

36
    /// Returns a color.
37
    pub fn get_color(&self, index: usize) -> Result<Color> {
3✔
38
        if !self.is_index_valid(index) {
3✔
39
            return Err(CommonError::new_invalid_index(index, self.lenght()));
1✔
40
        }
2✔
41

2✔
42
        Ok(self.colors[index])
2✔
43
    }
3✔
44

45
    /// Sets a color.
46
    pub fn set_color(&mut self, index: usize, color: Color) -> Result<()> {
2✔
47
        if !self.is_index_valid(index) {
2✔
48
            return Err(CommonError::new_invalid_index(index, self.lenght()));
1✔
49
        }
1✔
50

1✔
51
        self.colors[index] = color;
1✔
52

1✔
53
        Ok(())
1✔
54
    }
2✔
55

56
    /// Returns an iterator over all palette pixels.
57
    pub fn iter(&self) -> PaletteColorIter {
2✔
58
        self.colors.iter()
2✔
59
    }
2✔
60

61
    /// Returns a mutable iterator over all palette pixels.
62
    pub fn iter_mut(&mut self) -> PaletteColorIterMut {
1✔
63
        self.colors.iter_mut()
1✔
64
    }
1✔
65

66
    fn is_index_valid(&self, index: usize) -> bool {
5✔
67
        index < self.lenght()
5✔
68
    }
5✔
69
}
70

71
impl Default for Palette {
72
    /// Creates a Palette with all colors set to black.
73
    fn default() -> Self {
9✔
74
        Self {
9✔
75
            colors: vec![Color::default(); NUM_COLORS_IN_PALETTE],
9✔
76
        }
9✔
77
    }
9✔
78
}
79

80
impl fmt::Debug for Palette {
81
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1✔
82
        let data: Vec<&Color> = self.colors.iter().collect();
1✔
83

1✔
84
        f.debug_struct("Palette").field("colors", &data).finish()
1✔
85
    }
1✔
86
}
87

88
impl From<&[Color]> for Palette {
NEW
89
    fn from(colors: &[Color]) -> Self {
×
NEW
90
        Self {
×
NEW
91
            colors: colors.to_vec(),
×
NEW
92
        }
×
NEW
93
    }
×
94
}
95

96
#[cfg(test)]
97
mod tests {
98
    use assert_matches::assert_matches;
99

100
    use super::*;
101

102
    #[test]
103
    fn test_palette_default() {
1✔
104
        let palette = Palette::default();
1✔
105
        assert_eq!(palette.colors.len(), NUM_COLORS_IN_PALETTE);
1✔
106
    }
1✔
107

108
    #[test]
109
    fn test_palette_len() {
1✔
110
        let palette = Palette::default();
1✔
111
        assert_eq!(palette.lenght(), NUM_COLORS_IN_PALETTE);
1✔
112
    }
1✔
113

114
    #[test]
115
    fn test_palette_get_color() {
1✔
116
        let palette = Palette::default();
1✔
117
        let color = Color::default();
1✔
118

1✔
119
        let result = palette.get_color(0);
1✔
120
        assert!(result.is_ok());
1✔
121
        assert_eq!(result.unwrap(), color);
1✔
122
    }
1✔
123

124
    #[test]
125
    fn test_palette_get_color_invalid_index() {
1✔
126
        let palette = Palette::default();
1✔
127
        let index = 16usize;
1✔
128

1✔
129
        let result = palette.get_color(index);
1✔
130
        assert!(result.is_err());
1✔
131
        assert_matches!(
1✔
132
            result.unwrap_err(),
1✔
133
            CommonError::InvalidIndex { index: i, lenght: l } if i == index && l == palette.lenght()
1✔
134
        );
1✔
135
    }
1✔
136

137
    #[test]
138
    fn test_palette_set_color() {
1✔
139
        let mut palette = Palette::default();
1✔
140
        let color = Color::new(255, 255, 255);
1✔
141

1✔
142
        let result = palette.set_color(0, color);
1✔
143
        assert!(result.is_ok());
1✔
144

145
        let result = palette.get_color(0);
1✔
146
        assert_eq!(result.unwrap(), color);
1✔
147
    }
1✔
148

149
    #[test]
150
    fn test_palette_set_color_invalid_index() {
1✔
151
        let mut palette = Palette::default();
1✔
152
        let color = Color::new(255, 255, 255);
1✔
153
        let index = 16usize;
1✔
154

1✔
155
        let result = palette.set_color(16, color);
1✔
156
        assert!(result.is_err());
1✔
157
        assert_matches!(
1✔
158
            result.unwrap_err(),
1✔
159
            CommonError::InvalidIndex { index: i, lenght: l } if i == index && l == palette.lenght()
1✔
160
        );
1✔
161
    }
1✔
162

163
    #[test]
164
    fn test_palette_iter() {
1✔
165
        let palette = Palette::default();
1✔
166
        let default_color = Color::default();
1✔
167

168
        for color in palette.iter() {
16✔
169
            assert_eq!(color, &default_color);
16✔
170
        }
171
    }
1✔
172

173
    #[test]
174
    fn test_palette_iter_mut() {
1✔
175
        let mut palette = Palette::default();
1✔
176
        let new_color = Color::new(255, 255, 255);
1✔
177

178
        for color in palette.iter_mut() {
16✔
179
            *color = new_color;
16✔
180
        }
16✔
181

182
        for color in palette.iter() {
16✔
183
            assert_eq!(color, &new_color);
16✔
184
        }
185
    }
1✔
186

187
    #[test]
188
    fn test_palette_debug() {
1✔
189
        let palette = Palette::default();
1✔
190
        let data: Vec<&Color> = palette.colors.iter().collect();
1✔
191

1✔
192
        let expected = format!("Palette {{ colors: {:?} }}", data);
1✔
193
        let result = format!("{:?}", palette);
1✔
194

1✔
195
        assert_eq!(result, expected);
1✔
196
    }
1✔
197
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc