This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

get the ATAG's to work properly

Ivan Ilak (Apr 18, 2023, 11:22 PM +0200) 64503ba4 ec282b5c

+502 -136
+4 -2
cmake/target.cmake
··· 5 5 OR ${model} STREQUAL "MODEL_1") 6 6 set(CPU arm1176jzf-s) 7 7 set(ARCH_DIR ${path}/arch/armv6) 8 + set(KERNEL kernel) 8 9 elseif(${model} STREQUAL "MODEL_2") 9 10 set(CPU cortex-a7) 10 11 set(ARCH_DIR ${path}/arch/armv7-a) 12 + set(KERNEL kernel7) 11 13 elseif(${model} STREQUAL "MODEL_3") 12 14 set(ARCH_DIR ${path}/arch/armv8-a) 13 15 set(CPU cortex-a53) 16 + set(KERNEL kernel7) 14 17 elseif(${model} STREQUAL "MODEL_4") 15 18 set(ARCH_DIR ${path}/arch/armv8-a) 16 19 set(CPU cortex-a72) 20 + set(KERNEL kernel8) 17 21 endif() 18 22 19 23 if(${model} STREQUAL "MODEL_0" ··· 48 52 if(${arch} STREQUAL "32") 49 53 set(ARCH "AARCH_32") 50 54 set(TOOLCHAIN_PREFIX arm-none-eabi-) 51 - set(KERNEL kernel7) 52 55 else() 53 56 set(ARCH "AARCH_64") 54 57 set(TOOLCHAIN_PREFIX aarch64-none-elf-) 55 - set(KERNEL kernel8) 56 58 endif() 57 59 58 60 set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/arm-gcc-toolchain.cmake")
+1 -1
os/CMakeLists.txt
··· 37 37 OUTPUT ${KERNEL}.img 38 38 DEPENDS ${KERNEL}.elf 39 39 COMMENT "Creating IMG-file form ELF-file" 40 - COMMAND ${CMAKE_OBJCOPY} ARGS -O binary ${BUILD_DIR}/${KERNEL}.elf ${BUILD_DIR}/${KERNEL}.img 40 + COMMAND ${CMAKE_OBJCOPY} ARGS -O binary ${BUILD_DIR}/${KERNEL}.elf ${BUILD_DIR}/${KERNEL}.img && mv ${BUILD_DIR}/${KERNEL}.img ${PROJECT_SOURCE_DIR}/export/ 41 41 ) 42 42 43 43 # Bind above objcopy custom command with `kernel_img` target.
+106 -24
os/include/kernel/atag.h
··· 3 3 #ifndef ATAG_H 4 4 #define ATAG_H 5 5 6 - typedef enum { 7 - NONE = 0x00000000, 8 - CORE = 0x54410001, 9 - MEM = 0x54410002, 10 - INITRD2 = 0x54420005, 11 - CMDLINE = 0x54410009, 12 - } atag_tag_t; 6 + void print_atags(uint32_t address); 7 + 8 + #define ATAG_NONE 0 9 + #define ATAG_CORE 0x54410001 10 + #define ATAG_MEM 0x54410002 11 + #define ATAG_VIDEOTEXT 0x54410003 12 + #define ATAG_RAMDISK 0x54410004 13 + #define ATAG_INITRD2 0x54420005 14 + #define ATAG_SERIAL 0x54410006 15 + #define ATAG_REVISION 0x54410007 16 + #define ATAG_VIDEOLFB 0x54410008 17 + #define ATAG_CMDLINE 0x54410009 18 + 19 + typedef struct atag_header 20 + { 21 + uint32_t size; /**< Size in words of this tag */ 22 + uint32_t tag_type; /**< Tag value */ 23 + } atag_header; 13 24 14 - typedef struct { 15 - uint32_t size; 16 - uint32_t start; 17 - } mem_t; 25 + /* ATAG_NONE ends the list of ATAGs */ 26 + typedef struct atag_none 27 + { 28 + /* No further data in this ATAG */ 29 + } atag_none; 30 + 31 + /* ATAG_CORE begins the list of ATAGs */ 32 + typedef struct atag_core 33 + { 34 + /* Optional entries below */ 35 + uint32_t flags; /**< Bit 0 - read only. Others unused */ 36 + uint32_t pagesize; /**< Page size */ 37 + uint32_t rootdevice; /**< Root device number */ 38 + } atag_core; 18 39 19 - typedef struct { 20 - uint32_t start; 21 - uint32_t size; 22 - } initrd2_t; 40 + /* ATAG_MEM defines a physical memory region */ 41 + typedef struct atag_mem 42 + { 43 + uint32_t size; /**< Size of region */ 44 + uint32_t address; /**< Address of start of region */ 45 + } atag_mem; 23 46 24 - typedef struct { 25 - char line[1]; 26 - } cmdline_t; 47 + /* ATAG_VIDEOTEXT defines a VGA text screen. Not relevant to a Raspberry Pi */ 27 48 28 - typedef struct atag { 29 - uint32_t tag_size; 30 - atag_tag_t tag; 49 + /* ATAG_RAMDISK defines an initial ramdisk - floppy images only? */ 50 + typedef struct atag_ramdisk 51 + { 52 + uint32_t flags; /**< Bit 0 = load, bit 1 = prompt */ 53 + uint32_t size; /**< Decompressed size in KB */ 54 + uint32_t start; /**< Start block of ram disk */ 55 + } atag_ramdisk; 56 + 57 + /* ATAG_INITRD2 - define physical location of ramdisk image */ 58 + typedef struct atag_initrd2 59 + { 60 + uint32_t address; /**< Address of ramdisk image */ 61 + uint32_t size; /**< Size of compressed(?) image */ 62 + } atag_initrd2; 63 + 64 + /* ATAG_SERIAL has the 64-bit serial number */ 65 + typedef struct atag_serial 66 + { 67 + uint32_t low; 68 + uint32_t high; 69 + } atag_serial; 70 + 71 + /* ATAG_REVISION - board revision number */ 72 + typedef struct atag_revision 73 + { 74 + uint32_t revision; 75 + } atag_revision; 76 + 77 + /* ATAG_VIDEOLFB - describes a framebuffer */ 78 + typedef struct atag_videolfb 79 + { 80 + uint16_t width; /**< Width of buffer */ 81 + uint16_t height; /**< Height */ 82 + uint16_t depth; /**< Bits/pixel */ 83 + uint16_t linelength; // ? 84 + uint32_t address; /**< Base address of buffer */ 85 + uint32_t size; /**< Size of buffer */ 86 + unsigned char redsize; /**< Number of red bits in each pixel */ 87 + unsigned char redpos; /**< Position of red bits in pixel */ 88 + unsigned char greensize; 89 + unsigned char greenpos; 90 + unsigned char bluesize; 91 + unsigned char bluepos; 92 + unsigned char reservedsize; /**< Number of reserved bits/pixel */ 93 + unsigned char reservedpos; /**< Position of reserved bits */ 94 + } atag_videolfb; 95 + 96 + /* ATAG_CMDLINE - kernel command line */ 97 + typedef struct atag_cmdline 98 + { 99 + char* commandline; /**< Multiple characters from here */ 100 + } atag_cmdline; 101 + 102 + 103 + typedef struct atag_t { 104 + atag_header header; 31 105 union { 32 - mem_t mem; 33 - initrd2_t initrd2; 34 - cmdline_t cmdline; 106 + atag_core core; 107 + atag_mem mem; 108 + atag_ramdisk ramdisk; 109 + atag_initrd2 initrd2; 110 + atag_serial serial; 111 + atag_revision revision; 112 + atag_videolfb videolfb; 113 + atag_cmdline cmdline; 35 114 }; 36 115 } atag_t; 116 + 117 + #define tag_next(t) ((atag_t *)((uint32_t *)(t) + (t)->header.size)) 118 + #define tag_size(type) ((sizeof(atag_header) + sizeof(type)) >> 2) 37 119 38 120 uint32_t get_mem_size(atag_t * atags); 39 121
+118 -5
os/src/kernel/atag.c
··· 1 1 #include "kernel/atag.h" 2 + #include "kernel/kerio.h" 3 + #include "common/stdlib.h" 2 4 3 - uint32_t get_mem_size(atag_t * tag) { 4 - while (tag->tag != NONE) { 5 - if (tag->tag == MEM) { 6 - return tag->mem.size; 5 + uint32_t get_mem_size(atag_t * atags) { 6 + while (atags->header.tag_type != ATAG_NONE) { 7 + if (atags->header.tag_type == ATAG_MEM) { 8 + return atags->mem.size; 7 9 } 8 - tag = (atag_t *)((uint32_t *)tag + tag->tag_size); // Cast back to avoid compiler warning 10 + atags = tag_next(atags); 9 11 } 10 12 return 0; 13 + } 14 + 15 + char *tohex(unsigned int value, unsigned int size) 16 + { 17 + static char buffer[9]; 18 + 19 + unsigned int offset; 20 + unsigned char ch; 21 + 22 + if(size!=1 && size!=2 && size!=4) 23 + return "error"; 24 + 25 + offset=size*2; 26 + 27 + buffer[offset] = 0; 28 + 29 + while(offset--) 30 + { 31 + ch = 48 + (value & 15); 32 + if(ch>=58) 33 + ch+=7; 34 + 35 + buffer[offset] = ch; 36 + value = value >> 4; 37 + } 38 + 39 + return buffer; 40 + } 41 + 42 + void print_atag_core(atag_t* atags) 43 + { 44 + printf(" Flags: %d\n", atags->core.flags); 45 + printf(" Page-size: %d\n", atags->core.pagesize); 46 + printf(" Root device: %d\n\n", atags->core.rootdevice); 47 + } 48 + 49 + void print_atag_mem(atag_t* atags) 50 + { 51 + printf(" Size: %d MB\n", atags->mem.size/1024/1024); 52 + printf(" Address: %d\n\n", atags->mem.address); 53 + } 54 + 55 + void print_atag_cmdline(atag_t* atags) 56 + { 57 + printf(" Command Line: "); 58 + printf(atags->cmdline.commandline); 59 + printf("\n\n"); 60 + } 61 + 62 + void print_atags(uint32_t address) 63 + { 64 + atag_t *atags = (atag_t *) address; 65 + unsigned int tag; 66 + 67 + puts("Reading ATAGs\n\n"); 68 + 69 + do 70 + { 71 + tag = atags->header.tag_type; 72 + puts(" ATAG at address 0x"); 73 + puts(tohex((unsigned int) atags, 4)); 74 + puts(" is "); 75 + puts(tohex(tag, 4)); 76 + 77 + switch(tag) 78 + { 79 + case 0: 80 + puts(" (ATAG_NONE)\n\n"); 81 + break; 82 + case ATAG_CORE: 83 + puts(" (ATAG_CORE)\n\n"); 84 + print_atag_core(atags); 85 + break; 86 + case ATAG_MEM: 87 + puts(" (ATAG_MEM)\n\n"); 88 + print_atag_mem(atags); 89 + break; 90 + case ATAG_VIDEOTEXT: 91 + puts(" (ATAG_VIDEOTEXT)\n"); 92 + break; 93 + case ATAG_RAMDISK: 94 + puts(" (ATAG_RAMDISK)\n"); 95 + // print_atag_ramdisk((struct atag_ramdisk *)atags); 96 + break; 97 + case ATAG_INITRD2: 98 + puts(" (ATAG_INITRD2)\n"); 99 + // print_atag_initrd2((struct atag_initrd2 *)atags); 100 + break; 101 + case ATAG_SERIAL: 102 + puts(" (ATAG_SERIAL)\n"); 103 + // print_atag_serial((struct atag_serial *)atags); 104 + break; 105 + case ATAG_REVISION: 106 + puts(" (ATAG_REVISION)\n"); 107 + // print_atag_revision((struct atag_revision *)atags); 108 + break; 109 + case ATAG_VIDEOLFB: 110 + puts(" (ATAG_VIDEOLFB)\n"); 111 + // print_atag_videolfb((struct atag_videolfb *)atags); 112 + break; 113 + case ATAG_CMDLINE: 114 + puts(" (ATAG_CMDLINE)\n\n"); 115 + print_atag_cmdline(atags); 116 + break; 117 + default: 118 + puts(" (UNKNOWN)\n"); 119 + return; 120 + } 121 + 122 + atags = tag_next(atags); 123 + } while(tag); 11 124 }
+1
os/src/kernel/kerio.c
··· 11 11 12 12 void putc(char c) { 13 13 gpu_putc(c); 14 + // uart_putc(c); 14 15 } 15 16 16 17 void puts(const char * str) {
+26 -12
os/src/kernel/kernel.c
··· 3 3 #include "common/stdlib.h" 4 4 #include "common/string.h" 5 5 6 - #include "kernel/memory.h" 7 - #include "kernel/kerio.h" 8 6 #include "kernel/atag.h" 9 7 #include "kernel/gpu.h" 8 + #include "kernel/kerio.h" 9 + #include "kernel/memory.h" 10 10 #include "kernel/uart.h" 11 11 12 12 /*! ··· 16 16 * @param atags Start of ATAGS 17 17 */ 18 18 #if defined(AARCH_32) 19 - void kernel_main(uint32_t r0, uint32_t r1, uint32_t atags) 19 + void kernel_main(uint32_t r0, uint32_t arm_m_type, uint32_t atags) 20 20 #else 21 21 void kernel_main(uint64_t dtb_ptr32, uint64_t x0, uint64_t x1, uint64_t x3) 22 22 #endif 23 23 { 24 + (void)r0; 25 + (void)arm_m_type; 24 26 25 - mem_init((atag_t *)atags); 26 - gpu_init(); 27 + // assume if atags not specified then they are at 0x100 28 + if(atags == 0x0) 29 + atags = 0x100; 27 30 28 - puts("Welcome to piOS!\n\n"); 31 + mem_init((atag_t *)atags); 32 + // uart_init(); 33 + gpu_init(); 29 34 30 - uint32_t mem_size = get_mem_size((atag_t *)atags); 31 - printf("RAM size: %d\n", mem_size/1024/1024); 35 + puts("Welcome to piOS!\n----------------\n\n"); 36 + 37 + uint32_t mem_size = get_mem_size((atag_t *)atags); 38 + printf("RAM size: %d MB\n\n", mem_size / 1024 / 1024); 39 + 40 + #ifdef AARCH_32 41 + printf("Architecture: AARCH_%d\n\n", 32); 42 + #else 43 + printf("Architecture: AARCH_%d\n\n", 64); 44 + #endif 32 45 33 - puts("I han d Lea gern!"); 34 - 35 - while(1) { 36 - putc(getc()); 46 + print_atags(atags); 47 + 48 + while(1) 49 + { 50 + putc(getc()); 37 51 } 38 52 }
+2
os/src/kernel/memory.c
··· 1 1 #include "kernel/memory.h" 2 2 #include "kernel/atag.h" 3 3 #include "common/stdlib.h" 4 + #include "kernel/kerio.h" 4 5 #include <stdint.h> 5 6 #include <stddef.h> 6 7 ··· 41 42 42 43 // Get the total number of pages 43 44 mem_size = get_mem_size(atags); 45 + 44 46 num_pages = mem_size / PAGE_SIZE; 45 47 46 48 // Allocate space for all those pages' metadata. Start this block just after the kernel image is finished
+19 -2
tests/Makefile
··· 10 10 11 11 KER_SRC = ../os/src/kernel 12 12 COMMON_SRC = ../os/src/common 13 + TEST_SRC = ./src 13 14 14 15 KERNEL_VERSION = 0.1.0 15 16 ··· 33 34 mkdir -p $(@D) 34 35 clang $(SFLAGS) $(CFLAGS) -I$(INC_DIR) -DAARCH_32 -DMODEL_1 -c $< -o $@ 35 36 37 + ## Compile all test-dependencies 38 + $(BUILD_DIR)/%_c.o: ${TEST_SRC}/%.c 39 + mkdir -p $(@D) 40 + clang $(SFLAGS) $(CFLAGS) -I$(INC_DIR) -I/System/Volumes/Data/opt/homebrew/Cellar/cmocka/1.1.7/include -I${TEST_SRC}/.. -c $< -o $@ 41 + 36 42 ## Find all object files (from corresponding C, asm files) 37 43 ASM_FILES = $(wildcard $(ARCH_DIR)/*.S) 38 44 KER_C_FILES = $(wildcard $(KER_SRC)/*.c) 39 45 COMMON_C_FILES = $(wildcard $(COMMON_SRC)/*.c) 46 + TEST_C_FILES = $(wildcard $(TEST_SRC)/*.c) 40 47 41 48 OBJ_FILES = $(ASM_FILES:$(ARCH_DIR)/%.S=$(BUILD_DIR)/%_s.o) 42 49 OBJ_FILES += $(KER_C_FILES:$(KER_SRC)/%.c=$(BUILD_DIR)/%_c.o) 43 50 OBJ_FILES += $(COMMON_C_FILES:$(COMMON_SRC)/%.c=$(BUILD_DIR)/%_c.o) 51 + OBJ_FILES += $(TEST_C_FILES:$(TEST_SRC)/%.c=$(BUILD_DIR)/%_c.o) 44 52 45 53 ## Link all object files and create final image 46 54 build: $(OBJ_FILES) 47 - clang -o build/test.o -c test.c -I/System/Volumes/Data/opt/homebrew/Cellar/cmocka/1.1.7/include 48 - clang -o build/test build/test.o build/string_c.o build/stdlib_c.o -L/System/Volumes/Data/opt/homebrew/Cellar/cmocka/1.1.7/lib -lcmocka 55 + clang -o build/test.o -c test.c -I/System/Volumes/Data/opt/homebrew/Cellar/cmocka/1.1.7/include -Iinclude 56 + clang -o build/test \ 57 + build/test.o \ 58 + build/string_c.o \ 59 + build/stdlib_c.o \ 60 + build/atag_c.o \ 61 + build/test_string_c.o \ 62 + build/test_stdlib_c.o \ 63 + build/test_atags_c.o \ 64 + -L/System/Volumes/Data/opt/homebrew/Cellar/cmocka/1.1.7/lib \ 65 + -lcmocka 49 66 50 67 # Clean rules 51 68 clean:
+13
tests/include/test_atags.h
··· 1 + #ifndef TEST_ATAGS_H 2 + #define TEST_ATAGS_H 3 + 4 + #include <stdarg.h> 5 + #include <stddef.h> 6 + #include <setjmp.h> 7 + #include <cmocka.h> 8 + 9 + #include <stdlib.h> 10 + 11 + void test_atags(void** state); 12 + 13 + #endif // TEST_ATAGS_H
+23
tests/include/test_stdlib.h
··· 1 + #ifndef TEST_STDLIB_H 2 + #define TEST_STDLIB_H 3 + 4 + #include <stdarg.h> 5 + #include <stddef.h> 6 + #include <setjmp.h> 7 + #include <cmocka.h> 8 + 9 + void test_div(void** state); 10 + 11 + void test_divmod(void** state); 12 + 13 + void test_itoa(void** state); 14 + 15 + void test_atoi(void** state); 16 + 17 + void test_memset(void** state); 18 + 19 + void test_bzero(void** state); 20 + 21 + void test_memcpy(void** state); 22 + 23 + #endif // TEST_STDLIB_H
+19
tests/include/test_string.h
··· 1 + #ifndef TEST_STRING_H 2 + #define TEST_STRING_H 3 + 4 + #include <stdarg.h> 5 + #include <stddef.h> 6 + #include <setjmp.h> 7 + #include <cmocka.h> 8 + 9 + void test_str_len(void** state); 10 + 11 + void test_str_compare(void** state); 12 + 13 + void test_str_copy(void** state); 14 + 15 + void test_str_concat(void** state); 16 + 17 + void test_str_reverse(void** state); 18 + 19 + #endif // TEST_STRING_H
+88
tests/src/test_atags.c
··· 1 + #include "include/test_atags.h" 2 + 3 + #include "../os/include/kernel/atag.h" 4 + 5 + static atag_t *params; /* used to point at the current tag */ 6 + 7 + static void setup_core_tag(uint32_t *loc, long pagesize) 8 + { 9 + params = (atag_t *)loc; /* Initialise parameters to start at given address */ 10 + 11 + params->header.tag_type = ATAG_CORE; /* start with the core tag */ 12 + params->header.size = tag_size(atag_core); /* size the tag */ 13 + 14 + params->core.flags = 1; /* ensure read-only */ 15 + params->core.pagesize = pagesize; /* systems pagesize (4k) */ 16 + params->core.rootdevice 17 + = 0; /* zero root device (typicaly overidden from commandline )*/ 18 + 19 + params = tag_next(params); /* move pointer to next tag */ 20 + } 21 + 22 + static void setup_mem_tag(uint32_t start, uint32_t len) 23 + { 24 + params->header.tag_type = ATAG_MEM; /* Memory tag */ 25 + params->header.size = tag_size(atag_mem); /* size tag */ 26 + 27 + params->mem.address = start; /* Start of memory area (physical address) */ 28 + params->mem.size = len; /* Length of area */ 29 + 30 + params = tag_next(params); /* move pointer to next tag */ 31 + } 32 + 33 + static void setup_end_tag() 34 + { 35 + params->header.tag_type = ATAG_NONE; /* Empty tag ends list */ 36 + params->header.size = 0; /* zero length */ 37 + } 38 + 39 + void setup_atags(uint32_t *loc) 40 + { 41 + setup_core_tag(loc, 4096); 42 + setup_mem_tag(0, 4096); 43 + setup_end_tag(); 44 + } 45 + 46 + void test_ram_calculation(uint32_t *atags) 47 + { 48 + assert_int_equal(4096, get_mem_size((atag_t *)atags)); 49 + } 50 + 51 + void test_atag_core(atag_t *atags) 52 + { 53 + assert_int_equal(atags->header.tag_type, ATAG_CORE); 54 + 55 + assert_int_equal(atags->core.flags, 1); 56 + assert_int_equal(atags->core.pagesize, 4096); 57 + assert_int_equal(atags->core.rootdevice, 0); 58 + } 59 + 60 + void test_atag_mem(atag_t *atags) 61 + { 62 + assert_int_equal(atags->header.tag_type, ATAG_MEM); 63 + 64 + assert_int_equal(atags->mem.size, 4096); 65 + assert_int_equal(atags->mem.address, 0); 66 + } 67 + 68 + void test_atag_end(atag_t *atags) 69 + { 70 + assert_int_equal(atags->header.tag_type, ATAG_NONE); 71 + } 72 + 73 + void test_atags(void **state) 74 + { 75 + (void)state; 76 + uint32_t *space_for_atags = calloc(1000, sizeof(uint32_t)); 77 + setup_atags(space_for_atags); 78 + 79 + atag_t *atags = (atag_t *)space_for_atags; 80 + test_ram_calculation(space_for_atags); 81 + test_atag_core(atags); 82 + atags = tag_next(atags); 83 + test_atag_mem(atags); 84 + atags = tag_next(atags); 85 + test_atag_end(atags); 86 + 87 + free(space_for_atags); // also free's atags 88 + }
+56
tests/src/test_string.c
··· 1 + #include "include/test_string.h" 2 + 3 + #include "../os/include/common/string.h" 4 + 5 + void test_str_len(void** state) { 6 + (void)state; 7 + assert_int_equal(2, strlen("th")); 8 + assert_int_equal(0, strlen("")); 9 + assert_int_equal(1, strlen("\n")); 10 + assert_int_equal(0, strlen("\0")); 11 + } 12 + 13 + void test_str_compare(void** state) { 14 + (void)state; 15 + assert_int_equal(0, strcmp("", "")); 16 + assert_int_equal(1, strcmp("b", "a")); 17 + assert_int_equal(-1, strcmp("a", "b")); 18 + } 19 + 20 + void test_str_copy(void** state) { 21 + (void)state; 22 + char test0[] = "test"; 23 + char dest0[5]; 24 + assert_string_equal(test0, strcpy(dest0, test0)); 25 + char test1[] = ""; 26 + char dest1[0]; 27 + assert_string_equal(test1, strcpy(dest1, test1)); 28 + char test2[] = "a\0"; 29 + char dest2[2]; 30 + assert_string_equal(test2, strcpy(dest2, test2)); 31 + char test3[] = "will this work\0"; 32 + char dest3[3]; 33 + assert_string_equal(test3, strcpy(dest3, test3)); 34 + } 35 + 36 + void test_str_concat(void** state){ 37 + (void)state; 38 + char result0[] = "Hello World!\0"; 39 + char dest0[20] = "Hello \0"; 40 + char* dest_final0 = strcat(dest0, "World!\0"); 41 + assert_string_equal(result0, dest_final0); 42 + char result1[] = "World!\0"; 43 + char dest1[20] = "\0"; 44 + assert_string_equal(result1, strcat(dest1, "World!")); 45 + char result2[] = "\0"; 46 + char dest2[20] = "\0"; 47 + assert_string_equal(result2, strcat(dest2, "\0")); 48 + } 49 + 50 + void test_str_reverse(void** state){ 51 + (void)state; 52 + char result[] = "Hello World!"; 53 + char dest[] = "!dlroW olleH"; 54 + strrev(dest); 55 + assert_string_equal(result, dest); 56 + }
+17 -15
tests/test.c
··· 5 5 6 6 #include <stdio.h> 7 7 8 - #include "test_string.h" 9 - #include "test_stdlib.h" 10 - 8 + #include "include/test_string.h" 9 + #include "include/test_stdlib.h" 10 + #include "include/test_atags.h" 11 11 12 - int main(int argc, char* argv[]) { 12 + int main(int argc, char *argv[]) 13 + { 13 14 const struct CMUnitTest tests_string[] = { 14 - cmocka_unit_test(test_str_len), 15 - cmocka_unit_test(test_str_compare), 16 - cmocka_unit_test(test_str_copy), 17 - cmocka_unit_test(test_str_concat), 15 + cmocka_unit_test(test_str_len), cmocka_unit_test(test_str_compare), 16 + cmocka_unit_test(test_str_copy), cmocka_unit_test(test_str_concat), 18 17 cmocka_unit_test(test_str_reverse), 19 18 }; 20 19 21 20 const struct CMUnitTest tests_stdlib[] = { 22 - cmocka_unit_test(test_div), 23 - cmocka_unit_test(test_divmod), 24 - cmocka_unit_test(test_itoa), 25 - cmocka_unit_test(test_atoi), 26 - cmocka_unit_test(test_memset), 27 - cmocka_unit_test(test_bzero), 21 + cmocka_unit_test(test_div), cmocka_unit_test(test_divmod), 22 + cmocka_unit_test(test_itoa), cmocka_unit_test(test_atoi), 23 + cmocka_unit_test(test_memset), cmocka_unit_test(test_bzero), 28 24 cmocka_unit_test(test_memcpy), 29 25 }; 30 26 27 + const struct CMUnitTest tests_atags[] = { 28 + cmocka_unit_test(test_atags), 29 + }; 30 + 31 31 int first = cmocka_run_group_tests(tests_string, NULL, NULL); 32 32 printf("\n"); 33 33 int second = cmocka_run_group_tests(tests_stdlib, NULL, NULL); 34 + printf("\n"); 35 + int third = cmocka_run_group_tests(tests_atags, NULL, NULL); 34 36 35 - return first && second; 37 + return first && second && third; 36 38 }
+9 -17
tests/test_stdlib.h tests/src/test_stdlib.c
··· 1 - #ifndef TEST_STDLIB_H 2 - #define TEST_STDLIB_H 3 - 4 - #include <stdarg.h> 5 - #include <stddef.h> 6 - #include <setjmp.h> 7 - #include <cmocka.h> 1 + #include "include/test_stdlib.h" 8 2 9 3 #include "../os/include/common/stdlib.h" 10 4 11 - static void test_div(void** state){ 5 + void test_div(void** state){ 12 6 assert_int_equal(0, div(1, 2)); 13 7 assert_int_equal(46, div(234, 5)); 14 8 } 15 9 16 - static void test_divmod(void** state){ 10 + void test_divmod(void** state){ 17 11 divmod_t res = divmod(234, 5); 18 12 assert_int_equal(res.div, 46); 19 13 assert_int_equal(res.mod, 4); 20 14 } 21 15 22 - static void test_itoa(void** state){ 16 + void test_itoa(void** state){ 23 17 assert_string_equal("26", itoa(26, 10)); 24 18 assert_string_equal("-26", itoa(-26, 10)); 25 19 assert_string_equal("0b11010", itoa(26, 2)); ··· 27 21 assert_string_equal("0x1a", itoa(26, 16)); 28 22 } 29 23 30 - static void test_atoi(void** state){ 24 + void test_atoi(void** state){ 31 25 assert_int_equal(26, atoi("26")); 32 26 assert_int_equal(-26, atoi("-26")); 33 27 } 34 28 35 - static void test_memset(void** state){ 29 + void test_memset(void** state){ 36 30 char str[50] = "I need a sentence to test this!"; 37 31 memset(str+5, '.', 8); 38 32 assert_string_equal(str, "I nee........ence to test this!"); ··· 45 39 assert_int_equal(0, arr[i]); 46 40 } 47 41 48 - static void test_bzero(void** state){ 42 + void test_bzero(void** state){ 49 43 int32_t arr[10]; 50 44 for (int32_t i=0; i<10; i++) 51 45 arr[i] = 1; ··· 56 50 assert_int_equal(0, arr[i]); 57 51 } 58 52 59 - static void test_memcpy(void** state){ 53 + void test_memcpy(void** state){ 60 54 int32_t arr[10]; 61 55 for (int32_t i=0; i<10; i++) 62 56 arr[i] = 1; ··· 72 66 73 67 for (int32_t i=0; i<10; i++) 74 68 assert_int_equal(comp[i], arr[i]); 75 - } 76 - 77 - #endif // TEST_STDLIB_H 69 + }
-58
tests/test_string.h
··· 1 - #ifndef TEST_STRING_H 2 - #define TEST_STRING_H 3 - 4 - #include <stdarg.h> 5 - #include <stddef.h> 6 - #include <setjmp.h> 7 - #include <cmocka.h> 8 - 9 - #include "../os/include/common/string.h" 10 - 11 - static void test_str_len(void** state) { 12 - assert_int_equal(2, strlen("th")); 13 - assert_int_equal(0, strlen("")); 14 - assert_int_equal(1, strlen("\n")); 15 - assert_int_equal(0, strlen("\0")); 16 - } 17 - 18 - static void test_str_compare(void** state) { 19 - assert_int_equal(0, strcmp("", "")); 20 - assert_int_equal(1, strcmp("1", "")); 21 - assert_int_equal(-1, strcmp("", "a")); 22 - } 23 - 24 - static void test_str_copy(void** state) { 25 - char test0[] = "test"; 26 - char dest0[5]; 27 - assert_string_equal(test0, strcpy(dest0, test0)); 28 - char test1[] = ""; 29 - char dest1[0]; 30 - assert_string_equal(test1, strcpy(dest1, test1)); 31 - char test2[] = "a\0"; 32 - char dest2[2]; 33 - assert_string_equal(test2, strcpy(dest2, test2)); 34 - char test3[] = "will this work\0"; 35 - char dest3[3]; 36 - assert_string_equal(test3, strcpy(dest3, test3)); 37 - } 38 - 39 - static void test_str_concat(void** state){ 40 - char result0[] = "Hello World!"; 41 - char dest0[] = "Hello "; 42 - assert_string_equal(result0, strcat(dest0, "World!")); 43 - char result1[] = "World!\0"; 44 - char dest1[] = "\0"; 45 - assert_string_equal(result1, strcat(dest1, "World!")); 46 - char result2[] = "\0"; 47 - char dest2[] = "\0"; 48 - assert_string_equal(result2, strcat(dest2, "\0")); 49 - } 50 - 51 - static void test_str_reverse(void** state){ 52 - char result[] = "Hello World!"; 53 - char dest[] = "!dlroW olleH"; 54 - strrev(dest); 55 - assert_string_equal(result, dest); 56 - } 57 - 58 - #endif // TEST_STRING_H