Compare commits
2 Commits
fdc5b699bd
...
c1efc67106
| Author | SHA1 | Date | |
|---|---|---|---|
|
c1efc67106
|
|||
|
cb5111de82
|
4
Makefile
4
Makefile
@@ -7,7 +7,7 @@ CC = gcc
|
||||
SHELL := /bin/bash
|
||||
|
||||
INCLUDE := include
|
||||
CFLAGS = -Wall -I$(INCLUDE)
|
||||
CFLAGS = -Wall -I$(INCLUDE) -ggdb
|
||||
|
||||
BUILD_DIR := build
|
||||
|
||||
@@ -18,8 +18,10 @@ INCLUDE_INSTALL_PATH := /usr/local/include/lsort
|
||||
LIB_INSTALL_PATH := /usr/local/lib
|
||||
|
||||
STATIC_BUILD_FILES := $(STATIC_BUILD_DIR)/lsort.o \
|
||||
$(STATIC_BUILD_DIR)/types.o
|
||||
|
||||
DYNAMIC_BUILD_FILES := $(DYNAMIC_BUILD_DIR)/lsort.o \
|
||||
$(STATIC_BUILD_DIR)/types.o
|
||||
|
||||
TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#ifndef LSORT_H
|
||||
#define LSORT_H
|
||||
|
||||
int add(int x, int y);
|
||||
|
||||
|
||||
#endif
|
||||
34
include/types.h
Normal file
34
include/types.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef LSORT_TYPES_H
|
||||
#define LSORT_TYPES_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include "string.h"
|
||||
|
||||
typedef struct {
|
||||
const char *buf;
|
||||
size_t len;
|
||||
} lsort_string;
|
||||
|
||||
typedef struct {
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
int* items;
|
||||
} lsort_array_i;
|
||||
|
||||
lsort_array_i* lsort_create_int_array();
|
||||
void lsort_array_i_append(lsort_array_i* array, int number);
|
||||
int lsort_array_i_swap(lsort_array_i* array, int indexA, int indexB);
|
||||
|
||||
typedef struct {
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
lsort_array_i** items;
|
||||
} lsort_array_i_array;
|
||||
|
||||
// struct lsort_int_link {
|
||||
// int value;
|
||||
// lsort_array_i* next;
|
||||
// };
|
||||
|
||||
|
||||
#endif
|
||||
35
test/test.c
35
test/test.c
@@ -1,11 +1,34 @@
|
||||
#include "../include/lsort.h"
|
||||
#include "stdio.h"
|
||||
#include "../include/types.h"
|
||||
|
||||
int main(void) {
|
||||
int x = 5;
|
||||
int y = 2;
|
||||
int test_array_i_append(lsort_array_i* array) {
|
||||
|
||||
lsort_array_i_append(array, 25);
|
||||
lsort_array_i_append(array, 64);
|
||||
lsort_array_i_append(array, 34);
|
||||
lsort_array_i_append(array, 74);
|
||||
lsort_array_i_append(array, 99);
|
||||
lsort_array_i_append(array, 23);
|
||||
|
||||
printf("%d + %d = %d\n", x, y, add(x, y));
|
||||
if (array->items[5] != 23) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_array_i_swap(lsort_array_i* array) {
|
||||
lsort_array_i_swap(array, 0, 5);
|
||||
lsort_array_i_swap(array, 4, 2);
|
||||
lsort_array_i_swap(array, 2, 3);
|
||||
|
||||
if (array->items[0] != 23 || array->items[5] != 25 || array->items[4] != 34 || array->items[2] != 74 || array->items[3] != 99) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
lsort_array_i array = {0};
|
||||
|
||||
if (test_array_i_append(&array)) return 1;
|
||||
if (test_array_i_swap(&array)) return 2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
21
types.c
Normal file
21
types.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "include/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void lsort_array_i_append(lsort_array_i* array, int item) {
|
||||
if (array->count >= array->capacity) {
|
||||
if (array->capacity == 0) array->capacity = 8;
|
||||
else array->capacity *= 2;
|
||||
|
||||
array->items = realloc(array->items, array->capacity * sizeof(*array->items));
|
||||
}
|
||||
array->items[array->count++] = item;
|
||||
}
|
||||
|
||||
int lsort_array_i_swap(lsort_array_i* array, int indexA, int indexB) {
|
||||
if (indexA > array->count - 1 || indexB > array->count - 1) return 1;
|
||||
int temp = array->items[indexA];
|
||||
array->items[indexA] = array->items[indexB];
|
||||
array->items[indexB] = temp;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user