|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#![crate_type = "staticlib"] |
| 12 | +#![feature(c_variadic)] |
| 13 | +#![feature(libc)] |
| 14 | + |
| 15 | +extern crate libc; |
| 16 | + |
| 17 | +use libc::{c_char, c_double, c_int, c_long, c_longlong}; |
| 18 | +use std::ffi::VaList; |
| 19 | +use std::slice; |
| 20 | +use std::ffi::CStr; |
| 21 | + |
| 22 | +#[repr(C)] |
| 23 | +#[derive(Clone, Copy, Debug)] |
| 24 | +pub enum AnswerType { |
| 25 | + Double, |
| 26 | + Long, |
| 27 | + LongLong, |
| 28 | + Int, |
| 29 | + Byte, |
| 30 | + CStr, |
| 31 | + Skip, |
| 32 | +} |
| 33 | + |
| 34 | +#[repr(C)] |
| 35 | +pub union AnswerData { |
| 36 | + pub double: c_double, |
| 37 | + pub long: c_long, |
| 38 | + pub longlong: c_longlong, |
| 39 | + pub int: c_int, |
| 40 | + pub byte: c_char, |
| 41 | + pub cstr: *const c_char, |
| 42 | + pub skip_ty: AnswerType, |
| 43 | +} |
| 44 | + |
| 45 | +#[repr(C)] |
| 46 | +pub struct Answer { |
| 47 | + tag: AnswerType, |
| 48 | + data: AnswerData, |
| 49 | +} |
| 50 | + |
| 51 | +#[no_mangle] |
| 52 | +pub unsafe fn compare_answers(answers: &[Answer], mut ap: VaList) -> usize { |
| 53 | + for (i, answer) in answers.iter().enumerate() { |
| 54 | + match answer { |
| 55 | + Answer { tag: AnswerType::Double, data: AnswerData { double: d } } => { |
| 56 | + let tmp = ap.arg::<c_double>(); |
| 57 | + if d.floor() != tmp.floor() { |
| 58 | + println!("Double: {} != {}", d, tmp); |
| 59 | + return i + 1; |
| 60 | + } |
| 61 | + } |
| 62 | + Answer { tag: AnswerType::Long, data: AnswerData { long: l } } => { |
| 63 | + let tmp = ap.arg::<c_long>(); |
| 64 | + if *l != tmp { |
| 65 | + println!("Long: {} != {}", l, tmp); |
| 66 | + return i + 1; |
| 67 | + } |
| 68 | + } |
| 69 | + Answer { tag: AnswerType::LongLong, data: AnswerData { longlong: l } } => { |
| 70 | + let tmp = ap.arg::<c_longlong>(); |
| 71 | + if *l != tmp { |
| 72 | + println!("Long Long: {} != {}", l, tmp); |
| 73 | + return i + 1; |
| 74 | + } |
| 75 | + } |
| 76 | + Answer { tag: AnswerType::Int, data: AnswerData { int: n } } => { |
| 77 | + let tmp = ap.arg::<c_int>(); |
| 78 | + if *n != tmp { |
| 79 | + println!("Int: {} != {}", n, tmp); |
| 80 | + return i + 1; |
| 81 | + } |
| 82 | + } |
| 83 | + Answer { tag: AnswerType::Byte, data: AnswerData { byte: b } } => { |
| 84 | + let tmp = ap.arg::<c_char>(); |
| 85 | + if *b != tmp { |
| 86 | + println!("Byte: {} != {}", b, tmp); |
| 87 | + return i + 1; |
| 88 | + } |
| 89 | + } |
| 90 | + Answer { tag: AnswerType::CStr, data: AnswerData { cstr: c0 } } => { |
| 91 | + let c1 = ap.arg::<*const c_char>(); |
| 92 | + let cstr0 = CStr::from_ptr(*c0); |
| 93 | + let cstr1 = CStr::from_ptr(c1); |
| 94 | + if cstr0 != cstr1 { |
| 95 | + println!("C String: {:?} != {:?}", cstr0, cstr1); |
| 96 | + return i + 1; |
| 97 | + } |
| 98 | + } |
| 99 | + _ => { |
| 100 | + println!("Unknown type!"); |
| 101 | + return i + 1; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +#[no_mangle] |
| 109 | +pub unsafe extern "C" fn check_rust(argc: usize, answers: *const Answer, ap: VaList) -> usize { |
| 110 | + let slice = slice::from_raw_parts(answers, argc); |
| 111 | + compare_answers(slice, ap) |
| 112 | +} |
| 113 | + |
| 114 | +#[no_mangle] |
| 115 | +pub unsafe extern "C" fn check_rust_copy(argc: usize, answers: *const Answer, |
| 116 | + mut ap: VaList) -> usize { |
| 117 | + let slice = slice::from_raw_parts(answers, argc); |
| 118 | + let mut skip_n = 0; |
| 119 | + for (i, answer) in slice.iter().enumerate() { |
| 120 | + match answer { |
| 121 | + Answer { tag: AnswerType::Skip, data: AnswerData { skip_ty } } => { |
| 122 | + match skip_ty { |
| 123 | + AnswerType::Double => { ap.arg::<c_double>(); } |
| 124 | + AnswerType::Long => { ap.arg::<c_long>(); } |
| 125 | + AnswerType::LongLong => { ap.arg::<c_longlong>(); } |
| 126 | + AnswerType::Int => { ap.arg::<c_int>(); } |
| 127 | + AnswerType::Byte => { ap.arg::<c_char>(); } |
| 128 | + AnswerType::CStr => { ap.arg::<*const c_char>(); } |
| 129 | + _ => { return i; } |
| 130 | + }; |
| 131 | + } |
| 132 | + _ => { |
| 133 | + skip_n = i; |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + ap.copy(|ap| { |
| 140 | + compare_answers(&slice[skip_n..], ap) |
| 141 | + }) |
| 142 | +} |
0 commit comments