|
| 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 databend_common_base::vec_ext::VecExt; |
| 16 | +use databend_common_base::vec_ext::VecU8Ext; |
| 17 | +use quickcheck::quickcheck; |
| 18 | + |
| 19 | +#[test] |
| 20 | +fn test_remove_first() { |
| 21 | + fn prop(vec: Vec<i32>, item: i32) -> bool { |
| 22 | + let mut vec_clone = vec.clone(); |
| 23 | + let result = vec_clone.remove_first(&item); |
| 24 | + if let Some(pos) = vec.iter().position(|x| x == &item) { |
| 25 | + assert_eq!(result, Some(vec[pos])); |
| 26 | + let mut v = vec.clone(); |
| 27 | + v.remove(pos); |
| 28 | + vec_clone == v |
| 29 | + } else { |
| 30 | + assert_eq!(result, None); |
| 31 | + vec_clone == vec |
| 32 | + } |
| 33 | + } |
| 34 | + quickcheck(prop as fn(Vec<i32>, i32) -> bool); |
| 35 | +} |
| 36 | + |
| 37 | +#[test] |
| 38 | +fn test_push_unchecked() { |
| 39 | + fn prop(values: Vec<i32>) -> bool { |
| 40 | + let mut vec: Vec<i32> = Vec::with_capacity(values.len()); |
| 41 | + unsafe { |
| 42 | + for &value in &values { |
| 43 | + vec.push_unchecked(value); |
| 44 | + } |
| 45 | + } |
| 46 | + vec.len() == values.len() && vec.iter().zip(values.iter()).all(|(a, b)| a == b) |
| 47 | + } |
| 48 | + quickcheck(prop as fn(Vec<i32>) -> bool); |
| 49 | +} |
| 50 | + |
| 51 | +#[test] |
| 52 | +fn test_extend_from_slice_unchecked() { |
| 53 | + fn prop(values: Vec<i32>) -> bool { |
| 54 | + let mut vec: Vec<i32> = Vec::with_capacity(values.len() + 1); |
| 55 | + unsafe { |
| 56 | + vec.push_unchecked(1); // Ensure there's at least one element |
| 57 | + vec.extend_from_slice_unchecked(&values); |
| 58 | + } |
| 59 | + vec.len() == 1 + values.len() && vec[1..] == values[..] |
| 60 | + } |
| 61 | + quickcheck(prop as fn(Vec<i32>) -> bool); |
| 62 | +} |
| 63 | + |
| 64 | +#[test] |
| 65 | +fn test_store_value_uncheckd() { |
| 66 | + fn prop(value: i32) -> bool { |
| 67 | + let mut vec: Vec<u8> = Vec::with_capacity(std::mem::size_of::<i32>()); |
| 68 | + unsafe { |
| 69 | + vec.store_value_uncheckd(&value); |
| 70 | + } |
| 71 | + vec.len() == std::mem::size_of::<i32>() && vec[0..4] == value.to_le_bytes() |
| 72 | + } |
| 73 | + quickcheck(prop as fn(i32) -> bool); |
| 74 | +} |
0 commit comments