aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2017-10-12 20:01:28 -0700
committerJoe Wilm <joe@jwilm.com>2018-01-13 13:04:18 -0800
commit3619e3df2cc48efcb6548e0068ad81769c54ccf0 (patch)
tree6b169e7f880cb46d2f5b86d91773f7914e02ab4c
parent297a00b7aeef4b3dc3bcf5a475929b656a26df1d (diff)
downloadalacritty-3619e3df2cc48efcb6548e0068ad81769c54ccf0.tar.gz
alacritty-3619e3df2cc48efcb6548e0068ad81769c54ccf0.zip
Move grid Row and tests into submodules
This is part of some cleanup for the grid module as a whole.
-rw-r--r--src/grid/mod.rs (renamed from src/grid.rs)601
-rw-r--r--src/grid/row.rs186
-rw-r--r--src/grid/tests.rs188
3 files changed, 510 insertions, 465 deletions
diff --git a/src/grid.rs b/src/grid/mod.rs
index 2f4e3868..78614978 100644
--- a/src/grid.rs
+++ b/src/grid/mod.rs
@@ -20,15 +20,19 @@
//! time; rotation currently reorganize Vecs in the lines Vec, and indexing with
//! ranges is currently supported.
-use std::borrow::ToOwned;
use std::cmp::Ordering;
use std::collections::{VecDeque, vec_deque};
use std::iter::IntoIterator;
-use std::ops::{Deref, DerefMut, Range, RangeTo, RangeFrom, RangeFull, Index, IndexMut};
-use std::slice;
+use std::ops::{Deref, Range, RangeTo, RangeFrom, Index, IndexMut};
use index::{self, Point, Line, Column, IndexRange, RangeInclusive};
+mod row;
+pub use self::row::Row;
+
+#[cfg(test)]
+mod tests;
+
/// Convert a type to a linear index range.
pub trait ToRange {
fn to_range(&self) -> RangeInclusive<index::Linear>;
@@ -39,10 +43,11 @@ pub trait BidirectionalIterator: Iterator {
fn prev(&mut self) -> Option<Self::Item>;
}
+/// An item in the grid along with its Line and Column.
pub struct Indexed<T> {
+ pub inner: T,
pub line: Line,
pub column: Column,
- pub inner: T
}
impl<T> Deref for Indexed<T> {
@@ -127,158 +132,6 @@ impl<T: Clone> Grid<T> {
}
-/// A subset of lines in the grid
-///
-/// May be constructed using Grid::region(..)
-pub struct Region<'a, T: 'a> {
- start: Line,
- end: Line,
- raw: &'a VecDeque<Row<T>>,
-}
-
-/// A mutable subset of lines in the grid
-///
-/// May be constructed using Grid::region_mut(..)
-pub struct RegionMut<'a, T: 'a> {
- start: Line,
- end: Line,
- raw: &'a mut VecDeque<Row<T>>,
-}
-
-pub trait IndexRegion<I, T> {
- /// Get an immutable region of Self
- fn region<'a>(&'a self, _: I) -> Region<'a, T>;
-
- /// Get a mutable region of Self
- fn region_mut<'a>(&'a mut self, _: I) -> RegionMut<'a, T>;
-}
-
-impl<T> IndexRegion<Range<Line>, T> for Grid<T> {
- fn region(&self, index: Range<Line>) -> Region<T> {
- assert!(index.start < self.num_lines());
- assert!(index.end <= self.num_lines());
- assert!(index.start <= index.end);
- Region {
- start: index.start,
- end: index.end,
- raw: &self.raw
- }
- }
- fn region_mut(&mut self, index: Range<Line>) -> RegionMut<T> {
- assert!(index.start < self.num_lines());
- assert!(index.end <= self.num_lines());
- assert!(index.start <= index.end);
- RegionMut {
- start: index.start,
- end: index.end,
- raw: &mut self.raw
- }
- }
-}
-
-impl<T> IndexRegion<RangeTo<Line>, T> for Grid<T> {
- fn region(&self, index: RangeTo<Line>) -> Region<T> {
- assert!(index.end <= self.num_lines());
- Region {
- start: Line(0),
- end: index.end,
- raw: &self.raw
- }
- }
- fn region_mut(&mut self, index: RangeTo<Line>) -> RegionMut<T> {
- assert!(index.end <= self.num_lines());
- RegionMut {
- start: Line(0),
- end: index.end,
- raw: &mut self.raw
- }
- }
-}
-
-impl<T> IndexRegion<RangeFrom<Line>, T> for Grid<T> {
- fn region(&self, index: RangeFrom<Line>) -> Region<T> {
- assert!(index.start < self.num_lines());
- Region {
- start: index.start,
- end: self.num_lines(),
- raw: &self.raw
- }
- }
- fn region_mut(&mut self, index: RangeFrom<Line>) -> RegionMut<T> {
- assert!(index.start < self.num_lines());
- RegionMut {
- start: index.start,
- end: self.num_lines(),
- raw: &mut self.raw
- }
- }
-}
-
-pub struct RegionIter<'a, T: 'a> {
- end: Line,
- cur: Line,
- raw: &'a VecDeque<Row<T>>,
-}
-
-pub struct RegionIterMut<'a, T: 'a> {
- end: Line,
- cur: Line,
- raw: &'a mut VecDeque<Row<T>>,
-}
-
-impl<'a, T> IntoIterator for Region<'a, T> {
- type Item = &'a Row<T>;
- type IntoIter = RegionIter<'a, T>;
-
- fn into_iter(self) -> Self::IntoIter {
- RegionIter {
- end: self.end,
- cur: self.start,
- raw: self.raw
- }
- }
-}
-
-impl<'a, T> IntoIterator for RegionMut<'a, T> {
- type Item = &'a mut Row<T>;
- type IntoIter = RegionIterMut<'a, T>;
-
- fn into_iter(self) -> Self::IntoIter {
- RegionIterMut {
- end: self.end,
- cur: self.start,
- raw: self.raw
- }
- }
-}
-
-impl<'a, T> Iterator for RegionIter<'a, T> {
- type Item = &'a Row<T>;
- fn next(&mut self) -> Option<Self::Item> {
- if self.cur < self.end {
- let index = self.cur;
- self.cur += 1;
- Some(&self.raw[*index])
- } else {
- None
- }
- }
-}
-
-impl<'a, T> Iterator for RegionIterMut<'a, T> {
- type Item = &'a mut Row<T>;
- fn next(&mut self) -> Option<Self::Item> {
- if self.cur < self.end {
- let index = self.cur;
- self.cur += 1;
- unsafe {
- Some(&mut *(&mut self.raw[index.0] as *mut _))
- }
- } else {
- None
- }
- }
-}
impl<T> Grid<T> {
#[inline]
@@ -452,40 +305,6 @@ impl<'point, T> IndexMut<&'point Point> for Grid<T> {
}
}
-/// A row in the grid
-#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
-pub struct Row<T>(Vec<T>);
-
-impl<T: Clone> Row<T> {
- pub fn new(columns: index::Column, template: &T) -> Row<T> {
- Row(vec![template.to_owned(); *columns])
- }
-
- pub fn grow(&mut self, cols: index::Column, template: &T) {
- while self.len() != *cols {
- self.push(template.to_owned());
- }
- }
-}
-
-impl<T> Row<T> {
- pub fn shrink(&mut self, cols: index::Column) {
- while self.len() != *cols {
- self.pop();
- }
- }
-
- #[inline]
- pub fn cells(&self) -> slice::Iter<T> {
- self.0.iter()
- }
-
- #[inline]
- pub fn cells_mut(&mut self) -> slice::IterMut<T> {
- self.0.iter_mut()
- }
-}
-
impl<'a, T> IntoIterator for &'a Grid<T> {
type Item = &'a Row<T>;
type IntoIter = vec_deque::Iter<'a, Row<T>>;
@@ -496,135 +315,6 @@ impl<'a, T> IntoIterator for &'a Grid<T> {
}
}
-impl<'a, T> IntoIterator for &'a Row<T> {
- type Item = &'a T;
- type IntoIter = slice::Iter<'a, T>;
-
- #[inline]
- fn into_iter(self) -> slice::Iter<'a, T> {
- self.iter()
- }
-}
-
-impl<'a, T> IntoIterator for &'a mut Row<T> {
- type Item = &'a mut T;
- type IntoIter = slice::IterMut<'a, T>;
-
- #[inline]
- fn into_iter(self) -> slice::IterMut<'a, T> {
- self.iter_mut()
- }
-}
-
-impl<T> Deref for Row<T> {
- type Target = Vec<T>;
-
- #[inline]
- fn deref(&self) -> &Self::Target {
- &self.0
- }
-}
-
-impl<T> DerefMut for Row<T> {
- #[inline]
- fn deref_mut(&mut self) -> &mut Self::Target {
- &mut self.0
- }
-}
-
-impl<T> Index<index::Column> for Row<T> {
- type Output = T;
-
- #[inline]
- fn index(&self, index: index::Column) -> &T {
- &self.0[index.0]
- }
-}
-
-impl<T> IndexMut<index::Column> for Row<T> {
- #[inline]
- fn index_mut(&mut self, index: index::Column) -> &mut T {
- &mut self.0[index.0]
- }
-}
-
-macro_rules! row_index_range {
- ($range:ty) => {
- impl<T> Index<$range> for Row<T> {
- type Output = [T];
-
- #[inline]
- fn index(&self, index: $range) -> &[T] {
- &self.0[index]
- }
- }
-
- impl<T> IndexMut<$range> for Row<T> {
- #[inline]
- fn index_mut(&mut self, index: $range) -> &mut [T] {
- &mut self.0[index]
- }
- }
- }
-}
-
-row_index_range!(Range<usize>);
-row_index_range!(RangeTo<usize>);
-row_index_range!(RangeFrom<usize>);
-row_index_range!(RangeFull);
-
-// -----------------------------------------------------------------------------
-// Column ranges for Row
-// -----------------------------------------------------------------------------
-
-impl<T> Index<Range<index::Column>> for Row<T> {
- type Output = [T];
-
- #[inline]
- fn index(&self, index: Range<index::Column>) -> &[T] {
- &self.0[(index.start.0)..(index.end.0)]
- }
-}
-
-impl<T> IndexMut<Range<index::Column>> for Row<T> {
- #[inline]
- fn index_mut(&mut self, index: Range<index::Column>) -> &mut [T] {
- &mut self.0[(index.start.0)..(index.end.0)]
- }
-}
-
-impl<T> Index<RangeTo<index::Column>> for Row<T> {
- type Output = [T];
-
- #[inline]
- fn index(&self, index: RangeTo<index::Column>) -> &[T] {
- &self.0[..(index.end.0)]
- }
-}
-
-impl<T> IndexMut<RangeTo<index::Column>> for Row<T> {
- #[inline]
- fn index_mut(&mut self, index: RangeTo<index::Column>) -> &mut [T] {
- &mut self.0[..(index.end.0)]
- }
-}
-
-impl<T> Index<RangeFrom<index::Column>> for Row<T> {
- type Output = [T];
-
- #[inline]
- fn index(&self, index: RangeFrom<index::Column>) -> &[T] {
- &self.0[(index.start.0)..]
- }
-}
-
-impl<T> IndexMut<RangeFrom<index::Column>> for Row<T> {
- #[inline]
- fn index_mut(&mut self, index: RangeFrom<index::Column>) -> &mut [T] {
- &mut self.0[(index.start.0)..]
- }
-}
-
pub trait ClearRegion<R, T> {
fn clear_region<F: Fn(&mut T)>(&mut self, region: R, func: F);
}
@@ -647,178 +337,159 @@ clear_region_impl!(Range<index::Line>);
clear_region_impl!(RangeTo<index::Line>);
clear_region_impl!(RangeFrom<index::Line>);
-#[cfg(test)]
-mod tests {
- use super::{Grid, BidirectionalIterator};
- use index::{Point, Line, Column};
- #[test]
- fn grid_swap_lines_ok() {
- let mut grid = Grid::new(Line(10), Column(1), &0);
- info!("");
-
- // swap test ends
- grid[Line(0)][Column(0)] = 1;
- grid[Line(9)][Column(0)] = 2;
-
- assert_eq!(grid[Line(0)][Column(0)], 1);
- assert_eq!(grid[Line(9)][Column(0)], 2);
-
- grid.swap_lines(Line(0), Line(9));
+// =================================================================================================
+// Regions =========================================================================================
+// =================================================================================================
- assert_eq!(grid[Line(0)][Column(0)], 2);
- assert_eq!(grid[Line(9)][Column(0)], 1);
-
- // swap test mid
- grid[Line(4)][Column(0)] = 1;
- grid[Line(5)][Column(0)] = 2;
-
- info!("grid: {:?}", grid);
+/// A subset of lines in the grid
+///
+/// May be constructed using Grid::region(..)
+pub struct Region<'a, T: 'a> {
+ start: Line,
+ end: Line,
+ raw: &'a VecDeque<Row<T>>,
+}
- assert_eq!(grid[Line(4)][Column(0)], 1);
- assert_eq!(grid[Line(5)][Column(0)], 2);
+/// A mutable subset of lines in the grid
+///
+/// May be constructed using Grid::region_mut(..)
+pub struct RegionMut<'a, T: 'a> {
+ start: Line,
+ end: Line,
+ raw: &'a mut VecDeque<Row<T>>,
+}
- grid.swap_lines(Line(4), Line(5));
+pub trait IndexRegion<I, T> {
+ /// Get an immutable region of Self
+ fn region<'a>(&'a self, _: I) -> Region<'a, T>;
- info!("grid: {:?}", grid);
+ /// Get a mutable region of Self
+ fn region_mut<'a>(&'a mut self, _: I) -> RegionMut<'a, T>;
+}
- assert_eq!(grid[Line(4)][Column(0)], 2);
- assert_eq!(grid[Line(5)][Column(0)], 1);
+impl<T> IndexRegion<Range<Line>, T> for Grid<T> {
+ fn region(&self, index: Range<Line>) -> Region<T> {
+ assert!(index.start < self.num_lines());
+ assert!(index.end <= self.num_lines());
+ assert!(index.start <= index.end);
+ Region {
+ start: index.start,
+ end: index.end,
+ raw: &self.raw
+ }
}
-
- #[test]
- #[should_panic]
- fn grid_swap_lines_oob1() {
- let mut grid = Grid::new(Line(10), Column(1), &0);
- grid.swap_lines(Line(0), Line(10));
+ fn region_mut(&mut self, index: Range<Line>) -> RegionMut<T> {
+ assert!(index.start < self.num_lines());
+ assert!(index.end <= self.num_lines());
+ assert!(index.start <= index.end);
+ RegionMut {
+ start: index.start,
+ end: index.end,
+ raw: &mut self.raw
+ }
}
+}
- #[test]
- #[should_panic]
- fn grid_swap_lines_oob2() {
- let mut grid = Grid::new(Line(10), Column(1), &0);
- grid.swap_lines(Line(10), Line(0));
+impl<T> IndexRegion<RangeTo<Line>, T> for Grid<T> {
+ fn region(&self, index: RangeTo<Line>) -> Region<T> {
+ assert!(index.end <= self.num_lines());
+ Region {
+ start: Line(0),
+ end: index.end,
+ raw: &self.raw
+ }
}
-
- #[test]
- #[should_panic]
- fn grid_swap_lines_oob3() {
- let mut grid = Grid::new(Line(10), Column(1), &0);
- grid.swap_lines(Line(10), Line(10));
+ fn region_mut(&mut self, index: RangeTo<Line>) -> RegionMut<T> {
+ assert!(index.end <= self.num_lines());
+ RegionMut {
+ start: Line(0),
+ end: index.end,
+ raw: &mut self.raw
+ }
}
+}
- // Scroll up moves lines upwards
- #[test]
- fn scroll_up() {
- info!("");
-
- let mut grid = Grid::new(Line(10), Column(1), &0);
- for i in 0..10 {
- grid[Line(i)][Column(0)] = i;
+impl<T> IndexRegion<RangeFrom<Line>, T> for Grid<T> {
+ fn region(&self, index: RangeFrom<Line>) -> Region<T> {
+ assert!(index.start < self.num_lines());
+ Region {
+ start: index.start,
+ end: self.num_lines(),
+ raw: &self.raw
}
+ }
+ fn region_mut(&mut self, index: RangeFrom<Line>) -> RegionMut<T> {
+ assert!(index.start < self.num_lines());
+ RegionMut {
+ start: index.start,
+ end: self.num_lines(),
+ raw: &mut self.raw
+ }
+ }
+}
- info!("grid: {:?}", grid);
-
- grid.scroll_up(&(Line(0)..Line(10)), Line(2));
-
- info!("grid: {:?}", grid);
+pub struct RegionIter<'a, T: 'a> {
+ end: Line,
+ cur: Line,
+ raw: &'a VecDeque<Row<T>>,
+}
- let mut other = Grid::new(Line(10), Column(1), &9);
+pub struct RegionIterMut<'a, T: 'a> {
+ end: Line,
+ cur: Line,
+ raw: &'a mut VecDeque<Row<T>>,
+}
- other[Line(0)][Column(0)] = 2;
- other[Line(1)][Column(0)] = 3;
- other[Line(2)][Column(0)] = 4;
- other[Line(3)][Column(0)] = 5;
- other[Line(4)][Column(0)] = 6;
- other[Line(5)][Column(0)] = 7;
- other[Line(6)][Column(0)] = 8;
- other[Line(7)][Column(0)] = 9;
- other[Line(8)][Column(0)] = 0;
- other[Line(9)][Column(0)] = 1;
+impl<'a, T> IntoIterator for Region<'a, T> {
+ type Item = &'a Row<T>;
+ type IntoIter = RegionIter<'a, T>;
- for i in 0..10 {
- assert_eq!(grid[Line(i)][Column(0)], other[Line(i)][Column(0)]);
+ fn into_iter(self) -> Self::IntoIter {
+ RegionIter {
+ end: self.end,
+ cur: self.start,
+ raw: self.raw
}
}
+}
- // Scroll down moves lines downwards
- #[test]
- fn scroll_down() {
- info!("");
+impl<'a, T> IntoIterator for RegionMut<'a, T> {
+ type Item = &'a mut Row<T>;
+ type IntoIter = RegionIterMut<'a, T>;
- let mut grid = Grid::new(Line(10), Column(1), &0);
- for i in 0..10 {
- grid[Line(i)][Column(0)] = i;
+ fn into_iter(self) -> Self::IntoIter {
+ RegionIterMut {
+ end: self.end,
+ cur: self.start,
+ raw: self.raw
}
+ }
+}
- info!("grid: {:?}", grid);
-
- grid.scroll_down(&(Line(0)..Line(10)), Line(2));
-
- info!("grid: {:?}", grid);
-
- let mut other = Grid::new(Line(10), Column(1), &9);
-
- other[Line(0)][Column(0)] = 8;
- other[Line(1)][Column(0)] = 9;
- other[Line(2)][Column(0)] = 0;
- other[Line(3)][Column(0)] = 1;
- other[Line(4)][Column(0)] = 2;
- other[Line(5)][Column(0)] = 3;
- other[Line(6)][Column(0)] = 4;
- other[Line(7)][Column(0)] = 5;
- other[Line(8)][Column(0)] = 6;
- other[Line(9)][Column(0)] = 7;
-
- for i in 0..10 {
- assert_eq!(grid[Line(i)][Column(0)], other[Line(i)][Column(0)]);
+impl<'a, T> Iterator for RegionIter<'a, T> {
+ type Item = &'a Row<T>;
+ fn next(&mut self) -> Option<Self::Item> {
+ if self.cur < self.end {
+ let index = self.cur;
+ self.cur += 1;
+ Some(&self.raw[*index])
+ } else {
+ None
}
}
+}
- // Test that GridIterator works
- #[test]
- fn test_iter() {
- info!("");
-
- let mut grid = Grid::new(Line(5), Column(5), &0);
- for i in 0..5 {
- for j in 0..5 {
- grid[Line(i)][Column(j)] = i*5 + j;
+impl<'a, T> Iterator for RegionIterMut<'a, T> {
+ type Item = &'a mut Row<T>;
+ fn next(&mut self) -> Option<Self::Item> {
+ if self.cur < self.end {
+ let index = self.cur;
+ self.cur += 1;
+ unsafe {
+ Some(&mut *(&mut self.raw[index.0] as *mut _))
}
+ } else {
+ None
}
-
- info!("grid: {:?}", grid);
-
- let mut iter = grid.iter_from(Point {
- line: Line(0),
- col: Column(0),
- });
-
- assert_eq!(None, iter.prev());
- assert_eq!(Some(&1), iter.next());
- assert_eq!(Column(1), iter.cur.col);
- assert_eq!(Line(0), iter.cur.line);
-
- assert_eq!(Some(&2), iter.next());
- assert_eq!(Some(&3), iter.next());
- assert_eq!(Some(&4), iter.next());
-
- // test linewrapping
- assert_eq!(Some(&5), iter.next());
- assert_eq!(Column(0), iter.cur.col);
- assert_eq!(Line(1), iter.cur.line);
-
- assert_eq!(Some(&4), iter.prev());
- assert_eq!(Column(4), iter.cur.col);
- assert_eq!(Line(0), iter.cur.line);
-
-
- // test that iter ends at end of grid
- let mut final_iter = grid.iter_from(Point {
- line: Line(4),
- col: Column(4),
- });
- assert_eq!(None, final_iter.next());
- assert_eq!(Some(&23), final_iter.prev());
}
-
}
diff --git a/src/grid/row.rs b/src/grid/row.rs
new file mode 100644
index 00000000..8711d04f
--- /dev/null
+++ b/src/grid/row.rs
@@ -0,0 +1,186 @@
+// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! Defines the Row type which makes up lines in the grid
+
+use std::ops::{Deref, DerefMut, Index, IndexMut};
+use std::ops::{Range, RangeTo, RangeFrom, RangeFull};
+use std::slice;
+
+use index::Column;
+
+/// A row in the grid
+#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
+pub struct Row<T>(Vec<T>);
+
+impl<T: Clone> Row<T> {
+ pub fn new(columns: Column, template: &T) -> Row<T> {
+ Row(vec![template.to_owned(); *columns])
+ }
+
+ pub fn grow(&mut self, cols: Column, template: &T) {
+ while self.len() != *cols {
+ self.push(template.to_owned());
+ }
+ }
+}
+
+impl<T> Row<T> {
+ pub fn shrink(&mut self, cols: Column) {
+ while self.len() != *cols {
+ self.pop();
+ }
+ }
+
+ #[inline]
+ pub fn cells(&self) -> slice::Iter<T> {
+ self.0.iter()
+ }
+
+ #[inline]
+ pub fn cells_mut(&mut self) -> slice::IterMut<T> {
+ self.0.iter_mut()
+ }
+}
+
+
+impl<'a, T> IntoIterator for &'a Row<T> {
+ type Item = &'a T;
+ type IntoIter = slice::Iter<'a, T>;
+
+ #[inline]
+ fn into_iter(self) -> slice::Iter<'a, T> {
+ self.iter()
+ }
+}
+
+impl<'a, T> IntoIterator for &'a mut Row<T> {
+ type Item = &'a mut T;
+ type IntoIter = slice::IterMut<'a, T>;
+
+ #[inline]
+ fn into_iter(self) -> slice::IterMut<'a, T> {
+ self.iter_mut()
+ }
+}
+
+impl<T> Deref for Row<T> {
+ type Target = Vec<T>;
+
+ #[inline]
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+
+impl<T> DerefMut for Row<T> {
+ #[inline]
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.0
+ }
+}
+
+impl<T> Index<Column> for Row<T> {
+ type Output = T;
+
+ #[inline]
+ fn index(&self, index: Column) -> &T {
+ &self.0[index.0]
+ }
+}
+
+impl<T> IndexMut<Column> for Row<T> {
+ #[inline]
+ fn index_mut(&mut self, index: Column) -> &mut T {
+ &mut self.0[index.0]
+ }
+}
+
+macro_rules! row_index_range {
+ ($range:ty) => {
+ impl<T> Index<$range> for Row<T> {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: $range) -> &[T] {
+ &self.0[index]
+ }
+ }
+
+ impl<T> IndexMut<$range> for Row<T> {
+ #[inline]
+ fn index_mut(&mut self, index: $range) -> &mut [T] {
+ &mut self.0[index]
+ }
+ }
+ }
+}
+
+row_index_range!(Range<usize>);
+row_index_range!(RangeTo<usize>);
+row_index_range!(RangeFrom<usize>);
+row_index_range!(RangeFull);
+
+// -----------------------------------------------------------------------------
+// Column ranges for Row
+// -----------------------------------------------------------------------------
+
+impl<T> Index<Range<Column>> for Row<T> {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: Range<Column>) -> &[T] {
+ &self.0[(index.start.0)..(index.end.0)]
+ }
+}
+
+impl<T> IndexMut<Range<Column>> for Row<T> {
+ #[inline]
+ fn index_mut(&mut self, index: Range<Column>) -> &mut [T] {
+ &mut self.0[(index.start.0)..(index.end.0)]
+ }
+}
+
+impl<T> Index<RangeTo<Column>> for Row<T> {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: RangeTo<Column>) -> &[T] {
+ &self.0[..(index.end.0)]
+ }
+}
+
+impl<T> IndexMut<RangeTo<Column>> for Row<T> {
+ #[inline]
+ fn index_mut(&mut self, index: RangeTo<Column>) -> &mut [T] {
+ &mut self.0[..(index.end.0)]
+ }
+}
+
+impl<T> Index<RangeFrom<Column>> for Row<T> {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: RangeFrom<Column>) -> &[T] {
+ &self.0[(index.start.0)..]
+ }
+}
+
+impl<T> IndexMut<RangeFrom<Column>> for Row<T> {
+ #[inline]
+ fn index_mut(&mut self, index: RangeFrom<Column>) -> &mut [T] {
+ &mut self.0[(index.start.0)..]
+ }
+}
diff --git a/src/grid/tests.rs b/src/grid/tests.rs
new file mode 100644
index 00000000..169cefa0
--- /dev/null
+++ b/src/grid/tests.rs
@@ -0,0 +1,188 @@
+// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! Tests for the Gird
+
+use super::{Grid, BidirectionalIterator};
+use index::{Point, Line, Column};
+
+#[test]
+fn grid_swap_lines_ok() {
+ let mut grid = Grid::new(Line(10), Column(1), &0);
+ info!("");
+
+ // swap test ends
+ grid[Line(0)][Column(0)] = 1;
+ grid[Line(9)][Column(0)] = 2;
+
+ assert_eq!(grid[Line(0)][Column(0)], 1);
+ assert_eq!(grid[Line(9)][Column(0)], 2);
+
+ grid.swap_lines(Line(0), Line(9));
+
+ assert_eq!(grid[Line(0)][Column(0)], 2);
+ assert_eq!(grid[Line(9)][Column(0)], 1);
+
+ // swap test mid
+ grid[Line(4)][Column(0)] = 1;
+ grid[Line(5)][Column(0)] = 2;
+
+ info!("grid: {:?}", grid);
+
+ assert_eq!(grid[Line(4)][Column(0)], 1);
+ assert_eq!(grid[Line(5)][Column(0)], 2);
+
+ grid.swap_lines(Line(4), Line(5));
+
+ info!("grid: {:?}", grid);
+
+ assert_eq!(grid[Line(4)][Column(0)], 2);
+ assert_eq!(grid[Line(5)][Column(0)], 1);
+}
+
+#[test]
+#[should_panic]
+fn grid_swap_lines_oob1() {
+ let mut grid = Grid::new(Line(10), Column(1), &0);
+ grid.swap_lines(Line(0), Line(10));
+}
+
+#[test]
+#[should_panic]
+fn grid_swap_lines_oob2() {
+ let mut grid = Grid::new(Line(10), Column(1), &0);
+ grid.swap_lines(Line(10), Line(0));
+}
+
+#[test]
+#[should_panic]
+fn grid_swap_lines_oob3() {
+ let mut grid = Grid::new(Line(10), Column(1), &0);
+ grid.swap_lines(Line(10), Line(10));
+}
+
+// Scroll up moves lines upwards
+#[test]
+fn scroll_up() {
+ info!("");
+
+ let mut grid = Grid::new(Line(10), Column(1), &0);
+ for i in 0..10 {
+ grid[Line(i)][Column(0)] = i;
+ }
+
+ info!("grid: {:?}", grid);
+
+ grid.scroll_up(Line(0)..Line(10), Line(2));
+
+ info!("grid: {:?}", grid);
+
+ let mut other = Grid::new(Line(10), Column(1), &9);
+
+ other[Line(0)][Column(0)] = 2;
+ other[Line(1)][Column(0)] = 3;
+ other[Line(2)][Column(0)] = 4;
+ other[Line(3)][Column(0)] = 5;
+ other[Line(4)][Column(0)] = 6;
+ other[Line(5)][Column(0)] = 7;
+ other[Line(6)][Column(0)] = 8;
+ other[Line(7)][Column(0)] = 9;
+ other[Line(8)][Column(0)] = 0;
+ other[Line(9)][Column(0)] = 1;
+
+ for i in 0..10 {
+ assert_eq!(grid[Line(i)][Column(0)], other[Line(i)][Column(0)]);
+ }
+}
+
+// Scroll down moves lines downwards
+#[test]
+fn scroll_down() {
+ info!("");
+
+ let mut grid = Grid::new(Line(10), Column(1), &0);
+ for i in 0..10 {
+ grid[Line(i)][Column(0)] = i;
+ }
+
+ info!("grid: {:?}", grid);
+
+ grid.scroll_down(Line(0)..Line(10), Line(2));
+
+ info!("grid: {:?}", grid);
+
+ let mut other = Grid::new(Line(10), Column(1), &9);
+
+ other[Line(0)][Column(0)] = 8;
+ other[Line(1)][Column(0)] = 9;
+ other[Line(2)][Column(0)] = 0;
+ other[Line(3)][Column(0)] = 1;
+ other[Line(4)][Column(0)] = 2;
+ other[Line(5)][Column(0)] = 3;
+ other[Line(6)][Column(0)] = 4;
+ other[Line(7)][Column(0)] = 5;
+ other[Line(8)][Column(0)] = 6;
+ other[Line(9)][Column(0)] = 7;
+
+ for i in 0..10 {
+ assert_eq!(grid[Line(i)][Column(0)], other[Line(i)][Column(0)]);
+ }
+}
+
+// Test that GridIterator works
+#[test]
+fn test_iter() {
+ info!("");
+
+ let mut grid = Grid::new(Line(5), Column(5), &0);
+ for i in 0..5 {
+ for j in 0..5 {
+ grid[Line(i)][Column(j)] = i*5 + j;
+ }
+ }
+
+ info!("grid: {:?}", grid);
+
+ let mut iter = grid.iter_from(Point {
+ line: Line(0),
+ col: Column(0),
+ });
+
+ assert_eq!(None, iter.prev());
+ assert_eq!(Some(&1), iter.next());
+ assert_eq!(Column(1), iter.cur.col);
+ assert_eq!(Line(0), iter.cur.line);
+
+ assert_eq!(Some(&2), iter.next());
+ assert_eq!(Some(&3), iter.next());
+ assert_eq!(Some(&4), iter.next());
+
+ // test linewrapping
+ assert_eq!(Some(&5), iter.next());
+ assert_eq!(Column(0), iter.cur.col);
+ assert_eq!(Line(1), iter.cur.line);
+
+ assert_eq!(Some(&4), iter.prev());
+ assert_eq!(Column(4), iter.cur.col);
+ assert_eq!(Line(0), iter.cur.line);
+
+
+ // test that iter ends at end of grid
+ let mut final_iter = grid.iter_from(Point {
+ line: Line(4),
+ col: Column(4),
+ });
+ assert_eq!(None, final_iter.next());
+ assert_eq!(Some(&23), final_iter.prev());
+}