Commit Diff


commit - 87c313418265b477d8931182e8e9e0990db4f3ba
commit + 3e66d05b2cdeea5b72b2ee7c116f5dd0cdd1c270
blob - 0df18491956c216ee363ae7f2e0513b0746ab1fd
blob + 569e4c944bc2541a6fd5ef8176a103a5ccf9eeec
--- .gitignore
+++ .gitignore
@@ -7,3 +7,4 @@ tags
 test/got*.diff
 test/verify.*
 test/results_test
+test/arraylist_test
blob - 73fc7617ad6907c5ba7138f41853f8e9e447a48e
blob + 112da0c2e234000d38b2dea5c3b1112fa045b7a0
--- include/arraylist.h
+++ include/arraylist.h
@@ -106,3 +106,10 @@
 			free((ARRAY_LIST).head); \
 		ARRAYLIST_INIT(ARRAY_LIST, (ARRAY_LIST).alloc_blocksize); \
 	} while(0)
+
+#define ARRAYLIST_FOREACH(ITEM_P, ARRAY_LIST) \
+	for ((ITEM_P) = (ARRAY_LIST).head; \
+	     (ITEM_P) - (ARRAY_LIST).head < (ARRAY_LIST).len; \
+	     (ITEM_P)++)
+
+#define ARRAYLIST_IDX(ITEM_P, ARRAY_LIST) ((ITEM_P) - (ARRAY_LIST).head)
blob - 092ddce16cbae3745517da6d77fcc1d961cdf83b
blob + 80da7713de1a9ce2e589b3fc26630156b496d557
--- test/Makefile
+++ test/Makefile
@@ -11,14 +11,14 @@ clean:
 CFLAGS = -fsanitize=address -fsanitize=undefined -g -O3
 CFLAGS += -Wstrict-prototypes -Wunused-variable
 
-SRCS=	results_test.c
-LIB=	../lib/libdiff.a
-
 # Compat sources
 CFLAGS+=       -I$(CURDIR)/../compat/include
 
-results_test: $(SRCS) $(LIB)
+results_test: results_test.c ../lib/libdiff.a
 	gcc $(CFLAGS) -I../include -I../lib -o $@ $^
 
+arraylist_test: arraylist_test.c ../lib/libdiff.a
+	gcc $(CFLAGS) -I../include -I../lib -o $@ $^
+
 ../lib/libdiff.a: ../lib/*.[hc] ../include/*.h
 	$(MAKE) -C ../lib
blob - /dev/null
blob + eb1fec8d558f037374b10b3f5abbb28a96728670 (mode 644)
--- /dev/null
+++ test/arraylist_test.c
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <arraylist.h>
+
+void test_basic(void)
+{
+	int *p;
+	ARRAYLIST(int) list;
+	ARRAYLIST_INIT(list, 2);
+
+#define dump() do {\
+		printf("(%d items)\n", list.len); \
+		ARRAYLIST_FOREACH(p, list) \
+			printf("[%d] %d\n", ARRAYLIST_IDX(p, list), *p); \
+		printf("\n"); \
+	} while(0)
+
+	dump();
+
+	ARRAYLIST_ADD(p, list);
+	*p = 100;
+	dump();
+
+	ARRAYLIST_ADD(p, list);
+	*p = 101;
+	dump();
+
+	ARRAYLIST_ADD(p, list);
+	*p = 102;
+	dump();
+
+#define insert_test(AT) do {\
+		printf("insert at [" #AT "]:\n"); \
+		ARRAYLIST_INSERT(p, list, AT); \
+		*p = AT; \
+		dump(); \
+	} while(0)
+
+	insert_test(list.len - 1);
+	insert_test(1);
+	insert_test(0);
+	insert_test(6);
+	insert_test(123);
+	insert_test(-42);
+
+	printf("clear:\n");
+	ARRAYLIST_CLEAR(list);
+	dump();
+
+	ARRAYLIST_FREE(list);
+}
+
+int main(void)
+{
+	test_basic();
+}
blob - /dev/null
blob + 52a26087ac130f45f285e35835bbe2e9f057e458 (mode 644)
--- /dev/null
+++ test/expect.arraylist_test
@@ -0,0 +1,74 @@
+(0 items)
+
+(1 items)
+[0] 100
+
+(2 items)
+[0] 100
+[1] 101
+
+(3 items)
+[0] 100
+[1] 101
+[2] 102
+
+insert at [list.len - 1]:
+(4 items)
+[0] 100
+[1] 101
+[2] 3
+[3] 102
+
+insert at [1]:
+(5 items)
+[0] 100
+[1] 1
+[2] 101
+[3] 3
+[4] 102
+
+insert at [0]:
+(6 items)
+[0] 0
+[1] 100
+[2] 1
+[3] 101
+[4] 3
+[5] 102
+
+insert at [6]:
+(7 items)
+[0] 0
+[1] 100
+[2] 1
+[3] 101
+[4] 3
+[5] 102
+[6] 6
+
+insert at [123]:
+(8 items)
+[0] 0
+[1] 100
+[2] 1
+[3] 101
+[4] 3
+[5] 102
+[6] 6
+[7] 123
+
+insert at [-42]:
+(9 items)
+[0] 0
+[1] 100
+[2] 1
+[3] 101
+[4] 3
+[5] 102
+[6] 6
+[7] 123
+[8] -42
+
+clear:
+(0 items)
+