Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a8a7782
mutest for leetcode
invalid-email-address Apr 22, 2022
b95fb5d
添加字符串测试
invalid-email-address Apr 23, 2022
14e8b9a
covertz
invalid-email-address Apr 23, 2022
1dcd771
modify comment
invalid-email-address Apr 23, 2022
798192f
Z 字形变换
invalid-email-address Apr 23, 2022
e80b34a
update runtests.sh
invalid-email-address Apr 23, 2022
4319e28
Z型变换
invalid-email-address Apr 23, 2022
fc30453
comment the refer
invalid-email-address Apr 23, 2022
4f8155f
Merge branch 'master' of https://github.com/hamwastp/liblcthw into le…
invalid-email-address Apr 23, 2022
4bc1b0f
valgrind 内存检测发现指针使用不当问题
invalid-email-address Apr 23, 2022
3e65f20
merge
invalid-email-address Apr 23, 2022
5b227bf
Update README.md
hamwastp Apr 23, 2022
210eaeb
Merge branch 'leetcodemutest' of https://github.com/hamwastp/liblcthw…
invalid-email-address Apr 23, 2022
2adf610
Leetcodemutest (#1)
hamwastp Apr 23, 2022
5881ee9
Update README.md
hamwastp Apr 23, 2022
6dc28d2
Update README.md
hamwastp Apr 23, 2022
02fd182
Update README.md
hamwastp Apr 23, 2022
c8fbce5
Update README.md
hamwastp Apr 23, 2022
0641cc1
消除gcc不必要的告警
invalid-email-address Apr 23, 2022
275d9d9
monitor file change
invalid-email-address Apr 23, 2022
1cbb744
Makefile
invalid-email-address Apr 23, 2022
c0f0e19
Leetcodemutest (#2)
hamwastp Apr 23, 2022
0fa780d
Update zigzagconversion.c
hamwastp Apr 23, 2022
b809bc6
Update minunit.h
hamwastp Apr 24, 2022
8b6af82
Merge branch 'master' of https://github.com/hamwastp/liblcthw into le…
invalid-email-address Apr 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
tests/*_tests
tests/*.log
build
tests/leetcode/*_tests
20 changes: 15 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
CC=gcc
CFLAGS=-g -std=c99 -O0 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
LDFLAGS=$(OPTLIBS)
PREFIX?=/usr/local

SOURCES=$(wildcard src/**/*.c src/*.c)
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))

TEST_SRC=$(wildcard tests/*_tests.c)
TEST_SRC=$(wildcard tests/*_tests.c tests/leetcode/*_tests.c)
TESTS=$(patsubst %.c,%,$(TEST_SRC))
TESTAREA=lcthw
TESTSUIT=all

TARGET=build/liblcthw.a

OS=$(shell lsb_release -si)
ifeq ($(OS),Ubuntu)
ifeq ($(OS),CentOS)
LDLIBS=-llcthw -lbsd -L./build -lm
endif

Expand All @@ -22,19 +25,26 @@ dev: CFLAGS=-g -Wall -Isrc -Wall -Wextra $(OPTFLAGS)
dev: all

$(TARGET): CFLAGS += -fPIC
$(TARGET): build $(OBJECTS)
$(TARGET): build cleanobj $(OBJECTS)
ar rcs $@ $(OBJECTS)
ranlib $@

build:
@mkdir -p build
@mkdir -p bin

cleanobj:
@echo clean test object and bin
@if [[ "$(TESTSUIT)" != "all" ]];then rm -rf src/$(TESTAREA)/$(TESTSUIT).o;fi
@if [[ "$(TESTSUIT)" != "all" ]];then rm -rf tests/$(TESTAREA)/$(TESTSUIT)_tests;fi


# The Unit Tests
# eg: make TESTAREA=leetcode TESTSUIT=romantransfer
.PHONY: tests
tests: LDLIBS += $(TARGET)
tests: $(TESTS)
sh ./tests/runtests.sh
sh ./tests/runtests.sh $(TESTAREA) $(TESTSUIT)

valgrind:
VALGRIND="valgrind --log-file=/tmp/valgrind-%p.log" $(MAKE)
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
LibLCTHW
LibLCTHW-With-Mutest-Leetcode
========

1)
This is the library that you finally create in my book Learn C The Hard Way found at:

http://c.learncodethehardway.org/

This code is usually in sync with the book, but sometimes it gets out of sync during editing sessions. If you find a difference or a bug, just submit your pull request and I'll take a look.

2)
learn c the hard way with mini unit test your leetcode

https://github.com/hamwastp/liblcthw-with-mutest-leetcode/blob/master/tests/leetcode/README.md
451 changes: 237 additions & 214 deletions src/lcthw/bstrlib.h

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions src/lcthw/darray_algos.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#include <lcthw/darray_algos.h>
#include <stdlib.h>

int DArray_qsort(DArray * array, DArray_compare cmp)
int DArray_qsort(DArray* array, DArray_compare cmp)
{
qsort(array->contents, DArray_count(array), sizeof(void *), cmp);
qsort(array->contents, DArray_count(array), sizeof(void*), cmp);
return 0;
}

int DArray_heapsort(DArray * array, DArray_compare cmp)
#pragma GCC diagnostic ignored "-Wunused-parameter"
int DArray_heapsort(DArray* array, DArray_compare cmp)
{
return heapsort(array->contents, DArray_count(array),
sizeof(void *), cmp);
//return heapsort(array->contents, DArray_count(array),
// sizeof(void *), cmp);
return 0;
}

int DArray_mergesort(DArray * array, DArray_compare cmp)
int DArray_mergesort(DArray* array, DArray_compare cmp)
{
return mergesort(array->contents, DArray_count(array),
sizeof(void *), cmp);
//return mergesort(array->contents, DArray_count(array),
// sizeof(void *), cmp);
return 0;
}
59 changes: 39 additions & 20 deletions src/lcthw/list_algos.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
#include <lcthw/list_algos.h>
#include <lcthw/dbg.h>

inline void ListNode_swap(ListNode * a, ListNode * b)
/* inline */ void ListNode_swap(ListNode *a, ListNode *b)
{
void *temp = a->value;
a->value = b->value;
b->value = temp;
}

int List_bubble_sort(List * list, List_compare cmp)
int List_bubble_sort(List *list, List_compare cmp)
{
int sorted = 1;

if (List_count(list) <= 1) {
return 0; // already sorted
if (List_count(list) <= 1)
{
return 0; // already sorted
}

do {
do
{
sorted = 1;
LIST_FOREACH(list, first, next, cur) {
if (cur->next) {
if (cmp(cur->value, cur->next->value) > 0) {
LIST_FOREACH(list, first, next, cur)
{
if (cur->next)
{
if (cmp(cur->value, cur->next->value) > 0)
{
ListNode_swap(cur, cur->next);
sorted = 0;
}
Expand All @@ -31,24 +36,33 @@ int List_bubble_sort(List * list, List_compare cmp)
return 0;
}

inline List *List_merge(List * left, List * right, List_compare cmp)
/* inline */ List *List_merge(List *left, List *right, List_compare cmp)
{
List *result = List_create();
void *val = NULL;

while (List_count(left) > 0 || List_count(right) > 0) {
if (List_count(left) > 0 && List_count(right) > 0) {
if (cmp(List_first(left), List_first(right)) <= 0) {
while (List_count(left) > 0 || List_count(right) > 0)
{
if (List_count(left) > 0 && List_count(right) > 0)
{
if (cmp(List_first(left), List_first(right)) <= 0)
{
val = List_shift(left);
} else {
}
else
{
val = List_shift(right);
}

List_push(result, val);
} else if (List_count(left) > 0) {
}
else if (List_count(left) > 0)
{
val = List_shift(left);
List_push(result, val);
} else if (List_count(right) > 0) {
}
else if (List_count(right) > 0)
{
val = List_shift(right);
List_push(result, val);
}
Expand All @@ -57,22 +71,27 @@ inline List *List_merge(List * left, List * right, List_compare cmp)
return result;
}

List *List_merge_sort(List * list, List_compare cmp)
List *List_merge_sort(List *list, List_compare cmp)
{
List *result = NULL;

if (List_count(list) <= 1) {
if (List_count(list) <= 1)
{
return list;
}

List *left = List_create();
List *right = List_create();
int middle = List_count(list) / 2;

LIST_FOREACH(list, first, next, cur) {
if (middle > 0) {
LIST_FOREACH(list, first, next, cur)
{
if (middle > 0)
{
List_push(left, cur->value);
} else {
}
else
{
List_push(right, cur->value);
}

Expand Down
6 changes: 3 additions & 3 deletions src/lcthw/list_algos.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include <lcthw/list.h>

typedef int (*List_compare) (const void *a, const void *b);
typedef int (*List_compare)(const void *a, const void *b);

int List_bubble_sort(List * list, List_compare cmp);
int List_bubble_sort(List *list, List_compare cmp);

List *List_merge_sort(List * list, List_compare cmp);
List *List_merge_sort(List *list, List_compare cmp);

#endif
39 changes: 18 additions & 21 deletions src/lcthw/sarray.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#undef NDEBUG
#include <lcthw/sarray.h>
#include <lcthw/dbg.h>
#include <lcthw/sarray.h>
#include <stdlib.h>


int SuffixArray_compare(void *thunk, const void *a, const void *b)
int SuffixArray_compare(void* thunk, const void* a, const void* b)
{
SuffixArray *sarry = thunk;
int a_at = *(int *)a;
int b_at = *(int *)b;
char *a_str = sarry->source + a_at;
char *b_str = sarry->source + b_at;
SuffixArray* sarry = thunk;
int a_at = *(int*)a;
int b_at = *(int*)b;
char* a_str = sarry->source + a_at;
char* b_str = sarry->source + b_at;

int a_len = sarry->length - a_at;
int b_len = sarry->length - b_at;
Expand All @@ -21,7 +20,7 @@ int SuffixArray_compare(void *thunk, const void *a, const void *b)
return cmp;
}

int SuffixArray_find_suffix(SuffixArray *sarry, char *data, int length)
int SuffixArray_find_suffix(SuffixArray* sarry, char* data, int length)
{
int min = 0;
int max = sarry->length;
Expand All @@ -33,24 +32,23 @@ int SuffixArray_find_suffix(SuffixArray *sarry, char *data, int length)
// BUG: wrong, need to use the minimum length
cmp = strncmp(SuffixArray_substr(sarry, mid), data, length);

if(cmp < 0) {
if (cmp < 0) {
min = mid + 1;
} else {
max = mid - 1;
}
} while(cmp != 0 && min < max);
} while (cmp != 0 && min < max);

if(cmp == 0) {
if (cmp == 0) {
return mid;
} else {
return -1;
}
}


SuffixArray *SuffixArray_create(char *data, int length)
SuffixArray* SuffixArray_create(char* data, int length)
{
SuffixArray *sarry = calloc(1, sizeof(SuffixArray));
SuffixArray* sarry = calloc(1, sizeof(SuffixArray));
check_mem(sarry);

sarry->source = malloc(length + 1);
Expand All @@ -64,29 +62,28 @@ SuffixArray *SuffixArray_create(char *data, int length)
check_mem(sarry->indices);

int i = 0;
for(i = 0; i < length; i++) {
for (i = 0; i < length; i++) {
sarry->indices[i] = i;
}

#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
qsort_r(sarry->indices, length, sizeof(int), sarry, SuffixArray_compare);

return sarry;

error:
if(sarry) {
if (sarry) {
SuffixArray_destroy(sarry);
}

return NULL;
}


void SuffixArray_destroy(SuffixArray *sarry)
void SuffixArray_destroy(SuffixArray* sarry)
{
if(sarry) {
if (sarry) {
free(sarry->source);
free(sarry->indices);
free(sarry);
}
}

Loading