Add more scanf tests

This commit is contained in:
oddcoder
2020-06-03 23:19:08 +02:00
parent 8973535fdc
commit 92d6735e3f
12 changed files with 66 additions and 35 deletions
+4 -2
View File
@@ -27,7 +27,8 @@ global_asm!(
);
// https://git.musl-libc.org/cgit/musl/tree/crt/aarch64/crti.s
#[cfg(target_arch = "aarch64")]
global_asm!(r#"
global_asm!(
r#"
.section .init
.global _init
.type _init,%function
@@ -45,7 +46,8 @@ global_asm!(r#"
mov x29,sp
// stp: "stores two doublewords from the first and second argument to memory addressed by addr"
// Body will be filled in by gcc and ended by crtn.o
"#);
"#
);
#[panic_handler]
#[linkage = "weak"]
+4 -2
View File
@@ -23,7 +23,8 @@ global_asm!(
);
// https://git.musl-libc.org/cgit/musl/tree/crt/aarch64/crtn.s
#[cfg(target_arch = "aarch64")]
global_asm!(r#"
global_asm!(
r#"
.section .init
// This happens after crti.o and gcc has inserted code
// ldp: "loads two doublewords from memory addressed by the third argument to the first and second"
@@ -35,7 +36,8 @@ global_asm!(r#"
// ldp: "loads two doublewords from memory addressed by the third argument to the first and second"
ldp x29,x30,[sp],#16
ret
"#);
"#
);
#[panic_handler]
#[linkage = "weak"]
+11 -16
View File
@@ -1,4 +1,4 @@
use super::{fseek_locked, FILE, SEEK_SET, ftell_locked};
use super::{fseek_locked, ftell_locked, FILE, SEEK_SET};
use crate::core_io::Read;
struct LookAheadBuffer {
buf: *const u8,
@@ -27,23 +27,22 @@ impl From<*const u8> for LookAheadBuffer {
}
}
struct LookAheadFile<'a> {
f: &'a mut FILE,
look_ahead: i64,
}
impl <'a> LookAheadFile<'a> {
impl<'a> LookAheadFile<'a> {
fn look_ahead(&mut self) -> Result<Option<u8>, i32> {
let buf = &mut [0];
let seek = unsafe{ ftell_locked(self.f)};
unsafe{fseek_locked(self.f, self.look_ahead, SEEK_SET)};
let seek = unsafe { ftell_locked(self.f) };
unsafe { fseek_locked(self.f, self.look_ahead, SEEK_SET) };
let ret = match self.f.read(buf) {
Ok(0) => Ok(None),
Ok(_) => Ok(Some(buf[0])),
Err(_) => Err(-1),
};
unsafe{fseek_locked(self.f, seek, SEEK_SET)};
unsafe { fseek_locked(self.f, seek, SEEK_SET) };
self.look_ahead += 1;
ret
}
@@ -53,17 +52,13 @@ impl <'a> LookAheadFile<'a> {
}
}
impl <'a> From<&'a mut FILE> for LookAheadFile<'a> {
impl<'a> From<&'a mut FILE> for LookAheadFile<'a> {
fn from(f: &'a mut FILE) -> LookAheadFile<'a> {
let look_ahead = unsafe{ftell_locked(f)} as i64;
LookAheadFile{
f,
look_ahead,
}
let look_ahead = unsafe { ftell_locked(f) } as i64;
LookAheadFile { f, look_ahead }
}
}
enum LookAheadReaderEnum<'a> {
FILE(LookAheadFile<'a>),
// (buffer, location)
@@ -72,7 +67,7 @@ enum LookAheadReaderEnum<'a> {
pub struct LookAheadReader<'a>(LookAheadReaderEnum<'a>);
impl <'a> LookAheadReader<'a> {
impl<'a> LookAheadReader<'a> {
pub fn lookahead1(&mut self) -> Result<Option<u8>, i32> {
match &mut self.0 {
LookAheadReaderEnum::FILE(f) => f.look_ahead(),
@@ -87,13 +82,13 @@ impl <'a> LookAheadReader<'a> {
}
}
impl <'a> From<&'a mut FILE> for LookAheadReader<'a> {
impl<'a> From<&'a mut FILE> for LookAheadReader<'a> {
fn from(f: &'a mut FILE) -> LookAheadReader {
LookAheadReader(LookAheadReaderEnum::FILE(f.into()))
}
}
impl <'a> From<*const u8> for LookAheadReader<'a > {
impl<'a> From<*const u8> for LookAheadReader<'a> {
fn from(buff: *const u8) -> LookAheadReader<'a> {
LookAheadReader(LookAheadReaderEnum::BUFFER(buff.into()))
}
+1 -2
View File
@@ -516,7 +516,6 @@ pub unsafe extern "C" fn fseek(stream: *mut FILE, offset: c_long, whence: c_int)
/// Seek to an offset `offset` from `whence`
#[no_mangle]
pub unsafe extern "C" fn fseeko(stream: *mut FILE, off: off_t, whence: c_int) -> c_int {
let mut stream = (*stream).lock();
fseek_locked(&mut *stream, off, whence)
}
@@ -1051,7 +1050,7 @@ pub unsafe extern "C" fn vsprintf(s: *mut c_char, format: *const c_char, ap: va_
pub unsafe extern "C" fn vfscanf(file: *mut FILE, format: *const c_char, ap: va_list) -> c_int {
let ret = {
let mut file = (*file).lock();
let f :&mut FILE = &mut *file;
let f: &mut FILE = &mut *file;
let reader: LookAheadReader = f.into();
scanf::scanf(reader, format, ap)
};
+5 -5
View File
@@ -1,14 +1,14 @@
use core::slice;
use crate::platform::{Pal, Sys, types::*};
use crate::platform::{types::*, Pal, Sys};
pub const GRND_NONBLOCK: c_uint = 1;
pub const GRND_RANDOM: c_uint = 2;
#[no_mangle]
pub unsafe extern "C" fn getrandom(buf: *mut c_void, buflen: size_t, flags: c_uint) -> ssize_t {
Sys::getrandom(slice::from_raw_parts_mut(
buf as *mut u8,
buflen as usize
), flags)
Sys::getrandom(
slice::from_raw_parts_mut(buf as *mut u8, buflen as usize),
flags,
)
}
+4 -4
View File
@@ -5,6 +5,10 @@
//! compatibility. So, this module will be a hellhole.
use super::super::{errno, types::*, Pal, PalPtrace, PalSignal, Sys};
#[cfg(target_arch = "aarch64")]
use crate::header::arch_aarch64_user::user_regs_struct;
#[cfg(target_arch = "x86_64")]
use crate::header::arch_x64_user::user_regs_struct;
use crate::{
c_str::CString,
fs::File,
@@ -12,10 +16,6 @@ use crate::{
io::{self, prelude::*},
sync::{Mutex, Once},
};
#[cfg(target_arch = "aarch64")]
use crate::header::arch_aarch64_user::user_regs_struct;
#[cfg(target_arch = "x86_64")]
use crate::header::arch_x64_user::user_regs_struct;
use alloc::collections::{btree_map::Entry, BTreeMap};
use core::mem;
+4 -1
View File
@@ -143,7 +143,10 @@ unsafe fn inner_get_name(
inner_af_unix(&buf[5..], address, address_len);
} else {
// Socket doesn't belong to any scheme
panic!("socket {:?} doesn't match either tcp, udp or chan schemes", str::from_utf8(buf));
panic!(
"socket {:?} doesn't match either tcp, udp or chan schemes",
str::from_utf8(buf)
);
}
Ok(0)
+5 -3
View File
@@ -68,12 +68,14 @@ fn clock_gettime() {
#[test]
fn getrandom() {
use crate::header::sys_random;
use crate::platform::types::ssize_t;
use crate::{header::sys_random, platform::types::ssize_t};
let mut arrays = [[0; 32]; 32];
for i in 1..arrays.len() {
assert_eq!(Sys::getrandom(&mut arrays[i], 0), arrays[i].len() as ssize_t);
assert_eq!(
Sys::getrandom(&mut arrays[i], 0),
arrays[i].len() as ssize_t
);
for j in 0..arrays.len() {
if i != j {
+1
View File
@@ -45,6 +45,7 @@ EXPECT_NAMES=\
stdio/ungetc_multiple \
stdio/ungetc_ftell \
stdio/fscanf_offby1 \
stdio/fscanf \
stdio/printf_neg_pad \
stdlib/a64l \
stdlib/alloc \
View File
+14
View File
@@ -0,0 +1,14 @@
1 2 3 9
9
16
35
48
92
109
152
201
241
276
293
299
301
+13
View File
@@ -0,0 +1,13 @@
//@ 1 2 3
//@ SS
#include <stdio.h>
int main() {
FILE *f = fopen("stdio/fscanf.c", "r");
int x, y, z;
fscanf(f, "//@ %d %d %d",&x , &y, &z);
printf("%d %d %d %ld\n", x, y, z, ftell(f));
while(-1 != fscanf(f, "%*[^\n]")) {
printf("%ld\n", ftell(f));
getc(f);
}
}