bool getbit(void *buf, int offset){ int byte = offset >> 3; nemu_assert(offset >= 0); nemu_assert(offset <=7); offset &= 7; uint8_t mask = 1 << offset; nemu_assert(byte < 2 && byte >= 0); return (((uint8_t *)buf)[byte] & mask) != 0; }
void setbit(void *buf, int offset, bool bit){ int byte = offset >> 3; nemu_assert(offset >= 0); nemu_assert(offset <=15); offset &= 7; uint8_t mask = 1 << offset; uint8_t * volatile p = buf + byte; *p = (bit == 0 ? (*p & ~mask) : (*p | mask)); }
int main() { uint8_t buf[2]; buf[0] = 0xaa; nemu_assert(getbit(buf, 0) == 0); nemu_assert(getbit(buf, 1) == 1); nemu_assert(getbit(buf, 2) == 0); nemu_assert(getbit(buf, 3) == 1); nemu_assert(getbit(buf, 4) == 0); nemu_assert(getbit(buf, 5) == 1); nemu_assert(getbit(buf, 6) == 0); nemu_assert(getbit(buf, 7) == 1); setbit(buf, 8, 1); setbit(buf, 9, 0); setbit(buf, 10, 1); setbit(buf, 11, 0); setbit(buf, 12, 1); setbit(buf, 13, 0); setbit(buf, 14, 1); setbit(buf, 15, 0); nemu_assert(buf[1] == 0x55); HIT_GOOD_TRAP; return 0; }
int main() { int i; for(i = 0; i < 15; i ++) { nemu_assert(switch_case(i - 1) == ans[i]); } nemu_assert(i == 15); HIT_GOOD_TRAP; return 0; }
int main() { int n; for(n = 4; n <= 100; n += 2) { nemu_assert(gotbaha(n) == 1); } nemu_assert(n == 102); HIT_GOOD_TRAP; return 0; }
int main() { int i; for(i = 0; i < 128; i ++) { nemu_assert(to_lower_case(i) == ans[i]); } nemu_assert(i == 128); HIT_GOOD_TRAP; return 0; }
int main() { int i; for(i = 2; i < N; i ++) { fib[i] = fib[i - 1] + fib[i - 2]; nemu_assert(fib[i] == ans[i]); } nemu_assert(i == N); HIT_GOOD_TRAP; return 0; }
void test() { TYPE x, y; int i, j; for (i = 0; i < data_size; i++) for (j = 0; j <= 64; j++) { x = data[i]; y = x >> j; nemu_assert(y == naive_shift_right(x, j)); nemu_assert(y == naive_naive_shift_right(x, j)); } }
int main() { sprintf(buf, "%s", "Hello world!\n"); nemu_assert(strcmp(buf, "Hello world!\n") == 0); sprintf(buf, "%d + %d = %d\n", 1, 1, 2); nemu_assert(strcmp(buf, "1 + 1 = 2\n") == 0); sprintf(buf, "%d + %d = %d\n", 2, 10, 12); nemu_assert(strcmp(buf, "2 + 10 = 12\n") == 0); HIT_GOOD_TRAP; return 0; }
int main() { int i, j, ans_idx = 0; for(i = 0; i < NR_DATA; i ++) { for(j = 0; j < NR_DATA; j ++) { nemu_assert(add(test_data[i], test_data[j]) == ans[ans_idx ++]); } } nemu_assert(i == NR_DATA); nemu_assert(j == NR_DATA); HIT_GOOD_TRAP; return 0; }
int main() { quick_sort(a, 0, N - 1); int i; for(i = 0; i < N; i ++) { nemu_assert(a[i] == i); } quick_sort(a, 0, N - 1); for(i = 0; i < N; i ++) { nemu_assert(a[i] == i); } return 0; }
int main() { int i, ans_idx = 0; for(i = 0; i < NR_DATA; i ++) { nemu_assert(if_else(test_data[i]) == ans[ans_idx ++]); } return 0; }
int main() { int i; for(i = 0; i < 15; i ++) { nemu_assert(switch_case(i - 1) == ans[i]); } return 0; }
int main() { FLOAT a = computeT(10, f2F(-1.0), f2F(1.0), f); FLOAT ans = f2F(0.551222); nemu_assert(Fabs(a - ans) < f2F(1e-4)); HIT_GOOD_TRAP; return 0; }
FLOAT f2F(float aa) { uint32_t a = *((uint32_t*)(&aa)); uint32_t val; int v_exp; FLOAT res; // +-zero if((a & 0x7FFFFFFF) == 0) return 0; // +-inf if((a & 0x7FFFFFFF) == 0x7F800000) return (FLOAT)((uint32_t)a | 0xFFFFFF); v_exp = (a & 0x7F800000) >> 23; val = (a & 0x7FFFFF) | 0x800000; // NaN if(v_exp == 0xFF) nemu_assert(0); v_exp = v_exp - 127; if(v_exp+16-23 > 0) { res = val << (v_exp+16-23); } else { res = val >> (-v_exp-16+23); } if(res == 0) return 0; res &= 0x7FFFFFFF; if(a & 0x80000000) res = -res; return res; }
int main() { int i, j, k, ans_idx = 0; int loop = 0; for(i = 0; i < NR_DATA; i ++) { for(j = 0; j < NR_DATA; j ++) { for(k = 0; k < NR_DATA; k ++) { nemu_assert(min3(test_data[i], test_data[j], test_data[k]) == ans[ans_idx ++]); loop ++; } } } nemu_assert(loop == NR_DATA * NR_DATA * NR_DATA); return 0; }
int main() { volatile int a = 1; a = 0; nemu_assert(a); return 0; }
int main() { int i, j, k; int loop = 0; for(i = 0; i < N; i ++) { for(j = 0; j < N; j ++) { c[i][j] = 0; for(k = 0; k < N; k ++) { c[i][j] += a[i][k] * b[k][j]; loop ++; } nemu_assert(c[i][j] == ans[i][j]); } } nemu_assert(loop == N * N * N); return 0; }
int main() { int i; for (i = 0; i < SZ; i++) nemu_assert(empty[i] == 0); HIT_GOOD_TRAP; return 0; }
int main() { uint8_t buf[2]; buf[0] = 0xaa; nemu_assert((buf[0]<<0 & 0x1) == 0); HIT_GOOD_TRAP; return 0; }
int main() { select_sort(); int i; for(i = 0; i < N; i ++) { nemu_assert(a[i] == i); } select_sort(); for(i = 0; i < N; i ++) { nemu_assert(a[i] == i); } HIT_GOOD_TRAP; return 0; }
uint32_t loader() { Elf32_Ehdr *elf; Elf32_Phdr *ph = NULL; uint8_t buf[4096]; #ifdef HAS_DEVICE ide_read(buf, ELF_OFFSET_IN_DISK, 4096); #else ramdisk_read(buf, ELF_OFFSET_IN_DISK, 4096); #endif elf = (void*)buf; /* TODO: fix the magic number with the correct one */ const uint32_t elf_magic = 0x7f454c46; uint32_t *p_magic = (void *)buf; nemu_assert(*p_magic == elf_magic); /* Load each program segment */ for(; true; ) { /* Scan the program header table, load each segment into memory */ if(ph->p_type == PT_LOAD) { //PT_LOAT=1, Loadable program segment /* TODO: read the content of the segment from the ELF file * to the memory region [VirtAddr, VirtAddr + FileSiz) */ /* TODO: zero the memory region * [VirtAddr + FileSiz, VirtAddr + MemSiz) */ #ifdef IA32_PAGE /* Record the program break for future use. */ extern uint32_t brk; uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1; if(brk < new_brk) { brk = new_brk; } #endif } } volatile uint32_t entry = elf->e_entry; #ifdef IA32_PAGE mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE); #ifdef HAS_DEVICE create_video_mapping(); #endif write_cr3(get_ucr3()); #endif return entry; }
int main() { int i; for(i = 0; i < 125; i ++) { nemu_assert(is_leap_year(i + 1890) == ans[i]); } HIT_GOOD_TRAP; return 0; }
int main() { volatile float a, b, c; a = 0.5; b = 1.5; c = 2.0; nemu_assert(a + b == c); HIT_GOOD_TRAP; return 0; }
int main() { int i; for(i = 0; i < 13; i ++) { f[i] = fact(i); nemu_assert(f[i] == ans[i]); } HIT_GOOD_TRAP; return 0; }
uint32_t loader() { Elf32_Ehdr *elf; Elf32_Phdr *ph = NULL; uint8_t buf[4096]; #ifdef HAS_DEVICE ide_read(buf, ELF_OFFSET_IN_DISK, 4096); #else ramdisk_read(buf, ELF_OFFSET_IN_DISK, 4096); #endif elf = (void*)buf; /* fix the magic number with the correct one */ const uint32_t elf_magic = 0x464c457f; uint32_t *p_magic = (void *)buf; nemu_assert(*p_magic == elf_magic); /* Load each program segment */ int i; for(i=0;i<elf->e_phnum;i++ ) { /* Scan the program header table, load each segment into memory */ ph=(void *)(elf->e_phoff + i * elf->e_phentsize + buf); if(ph->p_type == PT_LOAD) { //Log("success"); #ifdef IA32_PAGE /* Record the program break for future use. */ extern uint32_t brk; uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1; if(brk < new_brk) { brk = new_brk; } uint32_t pa=mm_malloc(ph->p_vaddr,ph->p_memsz); #endif #ifndef HAS_DEVICE ramdisk_read((void*)pa, ELF_OFFSET_IN_DISK + ph->p_offset, ph->p_filesz); #else ide_read((void*)pa, ELF_OFFSET_IN_DISK + ph->p_offset, ph->p_filesz); #endif memset((void*)(pa + ph->p_filesz), 0, ph->p_memsz - ph->p_filesz); } } volatile uint32_t entry = elf->e_entry; #ifdef IA32_PAGE mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE); #ifdef HAS_DEVICE create_video_mapping(); #endif write_cr3(get_ucr3()); #endif return entry; }
int main() { int i, j, ans_idx = 0; for(i = 0; i < NR_DATA; i ++) { for(j = 0; j < NR_DATA; j ++) { nemu_assert(add(test_data[i], test_data[j]) == ans[ans_idx ++]); } } return 0; }
int main() { int n, n2, n1, n0; int k = 0; for(n = 100; n < 1000; n ++) { n2 = n / 100; n1 = (n / 10) % 10; n0 = n % 10; if(n == cube(n2) + cube(n1) + cube(n0)) { nemu_assert(n == ans[k]); k ++; } } nemu_assert(k == 4); HIT_GOOD_TRAP; return 0; }
int main(){ /* for(c=-(0x8000);c<0x8000;++c){ nemu_assert(c==(F2int(int2F(c)))); } set_bp(); f = 2.0; unsigned int u = *(unsigned int *)&f; nemu_assert(u>0); set_bp(); */ a = f2F(20000.0); nemu_assert((int2F(20000)-a)<5); set_bp(); b = F2int(a); nemu_assert(2==b); HIT_GOOD_TRAP; return 0; }
int main() { int i,j,ans_idx = 0; for (i = 0;i < NR_DATA;i++) { for (j = i;j < NR_DATA;j++) { nemu_assert(ans[ans_idx++] == mul(test_data[i],test_data[j])); } } HIT_GOOD_TRAP; return 0; }