Skip to content

Commit 0138ad4

Browse files
committed
Add two tests for const generics array impls.
1 parent 6485b6b commit 0138ad4

File tree

3 files changed

+250
-0
lines changed

3 files changed

+250
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// run-pass
2+
3+
#![allow(dead_code)]
4+
#![allow(unused_variables)]
5+
6+
use std::borrow::Borrow;
7+
use std::borrow::BorrowMut;
8+
use std::collections::BTreeSet;
9+
use std::collections::HashSet;
10+
use std::convert::TryFrom;
11+
12+
#[derive(Copy, Clone, Debug)]
13+
struct MyBool(bool);
14+
15+
impl PartialEq<bool> for MyBool {
16+
fn eq(&self, other: &bool) -> bool {
17+
self.0 == *other
18+
}
19+
}
20+
21+
impl PartialEq<MyBool> for bool {
22+
fn eq(&self, other: &MyBool) -> bool {
23+
*self == other.0
24+
}
25+
}
26+
27+
macro_rules! boolean_check_with_len {
28+
($LEN:expr) => {{
29+
let a = [true; $LEN];
30+
let mut b = [true; $LEN];
31+
let mut c = [false; $LEN];
32+
let mut d = [false; $LEN];
33+
34+
// PartialEq, Debug
35+
assert_eq!(a, b);
36+
if $LEN > 0 {
37+
assert_ne!(a, c);
38+
} else {
39+
assert_eq!(a, c);
40+
}
41+
42+
// AsRef
43+
let a_slice: &[bool] = a.as_ref();
44+
45+
// AsMut
46+
let b_slice: &mut [bool] = b.as_mut();
47+
48+
// Borrow
49+
let c_slice: &[bool] = c.borrow();
50+
51+
// BorrowMut
52+
let d_slice: &mut [bool] = d.borrow_mut();
53+
54+
// TryFrom
55+
let e_ref: &[bool; $LEN] = TryFrom::try_from(a_slice).unwrap();
56+
57+
// TryFrom#2
58+
let f: [bool; $LEN] = TryFrom::try_from(a_slice).unwrap();
59+
60+
// TryFrom#3
61+
let g_mut: &mut [bool; $LEN] = TryFrom::try_from(b_slice).unwrap();
62+
63+
// PartialEq, Eq, Hash
64+
let h: HashSet<[bool; $LEN]> = HashSet::new();
65+
66+
// PartialEq, Eq, PartialOrd, Ord
67+
let i: BTreeSet<[bool; $LEN]> = BTreeSet::new();
68+
69+
// IntoIterator#1
70+
for j in &a {
71+
let _ = j;
72+
}
73+
74+
// IntoIterator#2
75+
for k in &mut c {
76+
let _ = k;
77+
}
78+
79+
let l = [MyBool(true); $LEN];
80+
let l_slice: &[MyBool] = l.as_ref();
81+
82+
let mut m = [MyBool(false); $LEN];
83+
let m_slice: &mut [MyBool] = m.as_mut();
84+
85+
// PartialEq
86+
assert_eq!(a, l);
87+
assert_eq!(a, *l_slice);
88+
assert_eq!(a_slice, l);
89+
assert_eq!(a, l_slice);
90+
assert_eq!(c, m_slice);
91+
assert_eq!(m_slice, c);
92+
93+
// The currently omitted impls
94+
/*
95+
assert_eq!(a, &l);
96+
assert_eq!(&l, a);
97+
assert_eq!(a, &mut l);
98+
assert_eq!(&mut l, a);
99+
*/
100+
101+
/* Default is not using const generics now */
102+
/*
103+
assert_eq!(c, Default::default())
104+
*/
105+
}};
106+
}
107+
108+
fn check_boolean_array_less_than_32() {
109+
boolean_check_with_len!(0);
110+
boolean_check_with_len!(1);
111+
boolean_check_with_len!(2);
112+
boolean_check_with_len!(4);
113+
boolean_check_with_len!(8);
114+
boolean_check_with_len!(16);
115+
boolean_check_with_len!(31);
116+
boolean_check_with_len!(32);
117+
}
118+
119+
fn check_boolean_array_more_than_32() {
120+
boolean_check_with_len!(33);
121+
boolean_check_with_len!(64);
122+
boolean_check_with_len!(128);
123+
boolean_check_with_len!(256);
124+
boolean_check_with_len!(512);
125+
boolean_check_with_len!(1024);
126+
}
127+
128+
fn main() {
129+
// regression check
130+
check_boolean_array_less_than_32();
131+
// newly added
132+
check_boolean_array_more_than_32();
133+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// run-pass
2+
3+
#![feature(const_generics)]
4+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
5+
#![allow(dead_code)]
6+
#![allow(unused_variables)]
7+
8+
use std::borrow::Borrow;
9+
use std::borrow::BorrowMut;
10+
use std::collections::BTreeSet;
11+
use std::collections::HashSet;
12+
use std::convert::TryFrom;
13+
14+
#[derive(Copy, Clone, Debug)]
15+
struct MyBool(bool);
16+
17+
impl PartialEq<bool> for MyBool {
18+
fn eq(&self, other: &bool) -> bool {
19+
self.0 == *other
20+
}
21+
}
22+
23+
impl PartialEq<MyBool> for bool {
24+
fn eq(&self, other: &MyBool) -> bool {
25+
*self == other.0
26+
}
27+
}
28+
29+
fn check_boolean_array_with_const_generics<const LEN: usize>() {
30+
let a = [true; LEN];
31+
let mut b = [true; LEN];
32+
let mut c = [false; LEN];
33+
let mut d = [false; LEN];
34+
35+
// PartialEq, Debug
36+
assert_eq!(a, b);
37+
if LEN > 0 {
38+
assert_ne!(a, c);
39+
} else {
40+
assert_eq!(a, c);
41+
}
42+
43+
// AsRef
44+
let a_slice: &[bool] = a.as_ref();
45+
46+
// AsMut
47+
let b_slice: &mut [bool] = b.as_mut();
48+
49+
// Borrow
50+
let c_slice: &[bool] = c.borrow();
51+
52+
// BorrowMut
53+
let d_slice: &mut [bool] = d.borrow_mut();
54+
55+
// TryFrom
56+
let e_ref: &[bool; LEN] = TryFrom::try_from(a_slice).unwrap();
57+
58+
// TryFrom#2
59+
let f: [bool; LEN] = TryFrom::try_from(a_slice).unwrap();
60+
61+
// TryFrom#3
62+
let g_mut: &mut [bool; LEN] = TryFrom::try_from(b_slice).unwrap();
63+
64+
// PartialEq, Eq, Hash
65+
let h: HashSet<[bool; LEN]> = HashSet::new();
66+
67+
// PartialEq, Eq, PartialOrd, Ord
68+
let i: BTreeSet<[bool; LEN]> = BTreeSet::new();
69+
70+
// IntoIterator#1
71+
for j in &a {
72+
let _ = j;
73+
}
74+
75+
// IntoIterator#2
76+
for k in &mut c {
77+
let _ = k;
78+
}
79+
80+
let l = [MyBool(true); LEN];
81+
let l_slice: &[MyBool] = l.as_ref();
82+
83+
let mut m = [MyBool(false); LEN];
84+
let m_slice: &mut [MyBool] = m.as_mut();
85+
86+
// PartialEq
87+
assert_eq!(a, l);
88+
assert_eq!(a, *l_slice);
89+
assert_eq!(a_slice, l);
90+
assert_eq!(a, l_slice);
91+
assert_eq!(c, m_slice);
92+
assert_eq!(m_slice, c);
93+
94+
// The currently omitted impls
95+
/*
96+
assert_eq!(a, &l);
97+
assert_eq!(&l, a);
98+
assert_eq!(a, &mut l);
99+
assert_eq!(&mut l, a);
100+
*/
101+
102+
/* Default is not using const generics now */
103+
/*
104+
assert_eq!(c, Default::default())
105+
*/
106+
}
107+
108+
fn main() {
109+
check_boolean_array_with_const_generics::<{ 30 }>();
110+
check_boolean_array_with_const_generics::<{ 40 }>();
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/const-types.rs:3:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+

0 commit comments

Comments
 (0)