Skip to content

Commit 40624d1

Browse files
committed
Add builtin test
1 parent bf362c3 commit 40624d1

6 files changed

Lines changed: 275 additions & 114 deletions

File tree

tests/unit/builtin.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <assert.h>
2+
#include <limits.h>
3+
#if defined(__linux__)
4+
#include <byteswap.h>
5+
#endif
6+
7+
static void test_expect(void) {
8+
int x = 42;
9+
assert(__builtin_expect(x == 42, 1));
10+
}
11+
12+
static void test_ctz(void) {
13+
assert(__builtin_ctz(8U) == 3);
14+
assert(__builtin_ctz(1U) == 0);
15+
}
16+
17+
static void test_clz(void) {
18+
assert(__builtin_clz(1U) == 31);
19+
assert(__builtin_clz(0x80000000U) == 0);
20+
}
21+
22+
static void test_bswap16(void) {
23+
#if defined(__linux__)
24+
assert(bswap_16((unsigned short)0x1234) == 0x3412);
25+
#else
26+
assert(__builtin_bswap16((unsigned short)0x1234) == 0x3412);
27+
#endif
28+
}
29+
30+
static void test_bswap32(void) {
31+
#if defined(__linux__)
32+
assert(bswap_32(0x12345678U) == 0x78563412U);
33+
#else
34+
assert(__builtin_bswap32(0x12345678U) == 0x78563412U);
35+
#endif
36+
}
37+
38+
static void test_bswap64(void) {
39+
#if defined(__linux__)
40+
assert(bswap_64(0x0123456789ABCDEFULL) == 0xEFCDAB8967452301ULL);
41+
#else
42+
assert(__builtin_bswap64(0x0123456789ABCDEFULL) == 0xEFCDAB8967452301ULL);
43+
#endif
44+
}
45+
46+
static void test_ctzl(void) {
47+
assert(__builtin_ctzl(8UL) == 3);
48+
assert(__builtin_ctzl(1UL) == 0);
49+
}
50+
51+
static void test_popcountl(void) {
52+
assert(__builtin_popcountl(0UL) == 0);
53+
assert(__builtin_popcountl(0xFFUL) == 8);
54+
}
55+
56+
static void test_mul_overflow_long(void) {
57+
long r = 0;
58+
assert(!__builtin_mul_overflow(3L, 7L, &r));
59+
assert(r == 21);
60+
assert(__builtin_mul_overflow(LONG_MAX, 2L, &r));
61+
}
62+
63+
static void test_mul_overflow_long_long(void) {
64+
long long r = 0;
65+
assert(!__builtin_mul_overflow(1000LL, 1000LL, &r));
66+
assert(r == 1000000);
67+
assert(__builtin_mul_overflow(LLONG_MAX, 2LL, &r));
68+
}
69+
70+
#if defined(__x86_64__) || defined(__i386__)
71+
static void test_ia32_pause(void) {
72+
__builtin_ia32_pause();
73+
}
74+
#endif
75+
76+
int main(void) {
77+
test_expect();
78+
test_ctz();
79+
test_clz();
80+
test_bswap16();
81+
test_bswap32();
82+
test_bswap64();
83+
test_ctzl();
84+
test_popcountl();
85+
test_mul_overflow_long();
86+
test_mul_overflow_long_long();
87+
#if defined(__x86_64__) || defined(__i386__)
88+
test_ia32_pause();
89+
#endif
90+
return 0;
91+
}

tests/unit/builtin_mul_overflow.c

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/unit/out/refcount/builtin.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn test_expect_0() {
10+
let x: Value<i32> = Rc::new(RefCell::new(42));
11+
assert!((((((*x.borrow()) == 42) as i32) as i64) != 0));
12+
}
13+
pub fn test_ctz_1() {
14+
assert!((((8_u32.trailing_zeros() as i32 == 3) as i32) != 0));
15+
assert!((((1_u32.trailing_zeros() as i32 == 0) as i32) != 0));
16+
}
17+
pub fn test_clz_2() {
18+
assert!((((1_u32.leading_zeros() as i32 == 31) as i32) != 0));
19+
assert!((((2147483648_u32.leading_zeros() as i32 == 0) as i32) != 0));
20+
}
21+
pub fn test_bswap16_3() {
22+
assert!((((((4660 as u16).swap_bytes() as i32) == 13330) as i32) != 0));
23+
}
24+
pub fn test_bswap32_4() {
25+
assert!((((305419896_u32.swap_bytes() == 2018915346_u32) as i32) != 0));
26+
}
27+
pub fn test_bswap64_5() {
28+
assert!((((81985529216486895_u64.swap_bytes() == 17279655951921914625_u64) as i32) != 0));
29+
}
30+
pub fn test_ctzl_6() {
31+
assert!((((8_u64.trailing_zeros() as i32 == 3) as i32) != 0));
32+
assert!((((1_u64.trailing_zeros() as i32 == 0) as i32) != 0));
33+
}
34+
pub fn test_popcountl_7() {
35+
assert!((((0_u64.count_ones() as i32 == 0) as i32) != 0));
36+
assert!((((255_u64.count_ones() as i32 == 8) as i32) != 0));
37+
}
38+
pub fn test_mul_overflow_long_8() {
39+
let r: Value<i64> = Rc::new(RefCell::new(0_i64));
40+
assert!(
41+
((!{
42+
let (val, ovf) = 3_i64.overflowing_mul(7_i64);
43+
(r.as_pointer()).write(val);
44+
ovf
45+
} as i32)
46+
!= 0)
47+
);
48+
assert!(((((*r.borrow()) == 21_i64) as i32) != 0));
49+
assert!({
50+
let (val, ovf) = 9223372036854775807_i64.overflowing_mul(2_i64);
51+
(r.as_pointer()).write(val);
52+
ovf
53+
});
54+
}
55+
pub fn test_mul_overflow_long_long_9() {
56+
let r: Value<i64> = Rc::new(RefCell::new(0_i64));
57+
assert!(
58+
((!{
59+
let (val, ovf) = 1000_i64.overflowing_mul(1000_i64);
60+
(r.as_pointer()).write(val);
61+
ovf
62+
} as i32)
63+
!= 0)
64+
);
65+
assert!(((((*r.borrow()) == 1000000_i64) as i32) != 0));
66+
assert!({
67+
let (val, ovf) = 9223372036854775807_i64.overflowing_mul(2_i64);
68+
(r.as_pointer()).write(val);
69+
ovf
70+
});
71+
}
72+
pub fn test_ia32_pause_10() {
73+
std::hint::spin_loop();
74+
}
75+
pub fn main() {
76+
std::process::exit(main_0());
77+
}
78+
fn main_0() -> i32 {
79+
({ test_expect_0() });
80+
({ test_ctz_1() });
81+
({ test_clz_2() });
82+
({ test_bswap16_3() });
83+
({ test_bswap32_4() });
84+
({ test_bswap64_5() });
85+
({ test_ctzl_6() });
86+
({ test_popcountl_7() });
87+
({ test_mul_overflow_long_8() });
88+
({ test_mul_overflow_long_long_9() });
89+
({ test_ia32_pause_10() });
90+
return 0;
91+
}

tests/unit/out/refcount/builtin_mul_overflow.rs

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/unit/out/unsafe/builtin.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn test_expect_0() {
10+
let mut x: i32 = 42;
11+
assert!((((((x) == (42)) as i32) as i64) != 0));
12+
}
13+
pub unsafe fn test_ctz_1() {
14+
assert!(((((8_u32.trailing_zeros() as i32) == (3)) as i32) != 0));
15+
assert!(((((1_u32.trailing_zeros() as i32) == (0)) as i32) != 0));
16+
}
17+
pub unsafe fn test_clz_2() {
18+
assert!(((((1_u32.leading_zeros() as i32) == (31)) as i32) != 0));
19+
assert!(((((2147483648_u32.leading_zeros() as i32) == (0)) as i32) != 0));
20+
}
21+
pub unsafe fn test_bswap16_3() {
22+
assert!((((((4660 as u16).swap_bytes() as i32) == (13330)) as i32) != 0));
23+
}
24+
pub unsafe fn test_bswap32_4() {
25+
assert!(((((305419896_u32.swap_bytes()) == (2018915346_u32)) as i32) != 0));
26+
}
27+
pub unsafe fn test_bswap64_5() {
28+
assert!(((((81985529216486895_u64.swap_bytes()) == (17279655951921914625_u64)) as i32) != 0));
29+
}
30+
pub unsafe fn test_ctzl_6() {
31+
assert!(((((8_u64.trailing_zeros() as i32) == (3)) as i32) != 0));
32+
assert!(((((1_u64.trailing_zeros() as i32) == (0)) as i32) != 0));
33+
}
34+
pub unsafe fn test_popcountl_7() {
35+
assert!(((((0_u64.count_ones() as i32) == (0)) as i32) != 0));
36+
assert!(((((255_u64.count_ones() as i32) == (8)) as i32) != 0));
37+
}
38+
pub unsafe fn test_mul_overflow_long_8() {
39+
let mut r: i64 = 0_i64;
40+
assert!(
41+
((!{
42+
let (val, ovf) = 3_i64.overflowing_mul(7_i64);
43+
*(&mut r as *mut i64) = val;
44+
ovf
45+
} as i32)
46+
!= 0)
47+
);
48+
assert!(((((r) == (21_i64)) as i32) != 0));
49+
assert!({
50+
let (val, ovf) = 9223372036854775807_i64.overflowing_mul(2_i64);
51+
*(&mut r as *mut i64) = val;
52+
ovf
53+
});
54+
}
55+
pub unsafe fn test_mul_overflow_long_long_9() {
56+
let mut r: i64 = 0_i64;
57+
assert!(
58+
((!{
59+
let (val, ovf) = 1000_i64.overflowing_mul(1000_i64);
60+
*(&mut r as *mut i64) = val;
61+
ovf
62+
} as i32)
63+
!= 0)
64+
);
65+
assert!(((((r) == (1000000_i64)) as i32) != 0));
66+
assert!({
67+
let (val, ovf) = 9223372036854775807_i64.overflowing_mul(2_i64);
68+
*(&mut r as *mut i64) = val;
69+
ovf
70+
});
71+
}
72+
pub unsafe fn test_ia32_pause_10() {
73+
std::hint::spin_loop();
74+
}
75+
pub fn main() {
76+
unsafe {
77+
std::process::exit(main_0() as i32);
78+
}
79+
}
80+
unsafe fn main_0() -> i32 {
81+
(unsafe { test_expect_0() });
82+
(unsafe { test_ctz_1() });
83+
(unsafe { test_clz_2() });
84+
(unsafe { test_bswap16_3() });
85+
(unsafe { test_bswap32_4() });
86+
(unsafe { test_bswap64_5() });
87+
(unsafe { test_ctzl_6() });
88+
(unsafe { test_popcountl_7() });
89+
(unsafe { test_mul_overflow_long_8() });
90+
(unsafe { test_mul_overflow_long_long_9() });
91+
(unsafe { test_ia32_pause_10() });
92+
return 0;
93+
}

tests/unit/out/unsafe/builtin_mul_overflow.rs

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)