|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use crate::match_seq::errors::ConflictSeq; |
| 16 | + |
| 17 | +/// Check if the sequence number satisfies the condition. |
| 18 | +pub trait MatchSeqExt<T> { |
| 19 | + /// Match against a some value containing seq by checking if the seq satisfies the condition. |
| 20 | + fn match_seq(&self, sv: &T) -> Result<(), ConflictSeq>; |
| 21 | +} |
| 22 | + |
| 23 | +#[cfg(test)] |
| 24 | +mod tests { |
| 25 | + use crate::match_seq::errors::ConflictSeq; |
| 26 | + use crate::match_seq::MatchSeq; |
| 27 | + use crate::match_seq::MatchSeqExt; |
| 28 | + |
| 29 | + type SeqV = crate::seq_value::SeqV<u64, u64>; |
| 30 | + |
| 31 | + #[test] |
| 32 | + fn test_match_seq_match_seq_value() -> anyhow::Result<()> { |
| 33 | + assert_eq!(MatchSeq::GE(0).match_seq(&Some(SeqV::new(0, 1))), Ok(())); |
| 34 | + assert_eq!(MatchSeq::GE(0).match_seq(&Some(SeqV::new(1, 1))), Ok(())); |
| 35 | + |
| 36 | + // |
| 37 | + |
| 38 | + assert_eq!( |
| 39 | + MatchSeq::Exact(3).match_seq(&None::<SeqV>), |
| 40 | + Err(ConflictSeq::NotMatch { |
| 41 | + want: MatchSeq::Exact(3), |
| 42 | + got: 0 |
| 43 | + }) |
| 44 | + ); |
| 45 | + assert_eq!( |
| 46 | + MatchSeq::Exact(3).match_seq(&Some(SeqV::new(0, 1))), |
| 47 | + Err(ConflictSeq::NotMatch { |
| 48 | + want: MatchSeq::Exact(3), |
| 49 | + got: 0 |
| 50 | + }) |
| 51 | + ); |
| 52 | + assert_eq!( |
| 53 | + MatchSeq::Exact(3).match_seq(&Some(SeqV::new(2, 1))), |
| 54 | + Err(ConflictSeq::NotMatch { |
| 55 | + want: MatchSeq::Exact(3), |
| 56 | + got: 2 |
| 57 | + }) |
| 58 | + ); |
| 59 | + assert_eq!(MatchSeq::Exact(3).match_seq(&Some(SeqV::new(3, 1))), Ok(())); |
| 60 | + assert_eq!( |
| 61 | + MatchSeq::Exact(3).match_seq(&Some(SeqV::new(4, 1))), |
| 62 | + Err(ConflictSeq::NotMatch { |
| 63 | + want: MatchSeq::Exact(3), |
| 64 | + got: 4 |
| 65 | + }) |
| 66 | + ); |
| 67 | + |
| 68 | + // |
| 69 | + |
| 70 | + assert_eq!( |
| 71 | + MatchSeq::GE(3).match_seq(&None::<SeqV>), |
| 72 | + Err(ConflictSeq::NotMatch { |
| 73 | + want: MatchSeq::GE(3), |
| 74 | + got: 0 |
| 75 | + }) |
| 76 | + ); |
| 77 | + assert_eq!( |
| 78 | + MatchSeq::GE(3).match_seq(&Some(SeqV::new(0, 1))), |
| 79 | + Err(ConflictSeq::NotMatch { |
| 80 | + want: MatchSeq::GE(3), |
| 81 | + got: 0 |
| 82 | + }) |
| 83 | + ); |
| 84 | + assert_eq!( |
| 85 | + MatchSeq::GE(3).match_seq(&Some(SeqV::new(2, 1))), |
| 86 | + Err(ConflictSeq::NotMatch { |
| 87 | + want: MatchSeq::GE(3), |
| 88 | + got: 2 |
| 89 | + }) |
| 90 | + ); |
| 91 | + assert_eq!(MatchSeq::GE(3).match_seq(&Some(SeqV::new(3, 1))), Ok(())); |
| 92 | + assert_eq!(MatchSeq::GE(3).match_seq(&Some(SeqV::new(4, 1))), Ok(())); |
| 93 | + |
| 94 | + Ok(()) |
| 95 | + } |
| 96 | +} |
0 commit comments