summaryrefslogtreecommitdiff
path: root/test-mergesort.c
diff options
context:
space:
mode:
authorLibravatar Jiang Xin <worldhello.net@gmail.com>2012-04-28 15:54:37 +0800
committerLibravatar Jiang Xin <worldhello.net@gmail.com>2012-04-28 15:54:37 +0800
commit69c835701b507e336fd5354f9f3b05e42d20c07d (patch)
treecc6b079231ddc1ab3e04654cbdf8937ef0d6632b /test-mergesort.c
parentl10n: Add the German translation team and initialize de.po (diff)
parentThe sixth batch of topics graduated to 'master' (diff)
downloadtgif-69c835701b507e336fd5354f9f3b05e42d20c07d.tar.xz
Merge master branch for tracking l10n updates of next release
Use master branch to track l10n updates for git next release, while use maint branch to track l10n updates for git stable version.
Diffstat (limited to 'test-mergesort.c')
-rw-r--r--test-mergesort.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/test-mergesort.c b/test-mergesort.c
new file mode 100644
index 0000000000..3f388b4ce0
--- /dev/null
+++ b/test-mergesort.c
@@ -0,0 +1,52 @@
+#include "cache.h"
+#include "mergesort.h"
+
+struct line {
+ char *text;
+ struct line *next;
+};
+
+static void *get_next(const void *a)
+{
+ return ((const struct line *)a)->next;
+}
+
+static void set_next(void *a, void *b)
+{
+ ((struct line *)a)->next = b;
+}
+
+static int compare_strings(const void *a, const void *b)
+{
+ const struct line *x = a, *y = b;
+ return strcmp(x->text, y->text);
+}
+
+int main(int argc, const char **argv)
+{
+ struct line *line, *p = NULL, *lines = NULL;
+ struct strbuf sb = STRBUF_INIT;
+
+ for (;;) {
+ if (strbuf_getwholeline(&sb, stdin, '\n'))
+ break;
+ line = xmalloc(sizeof(struct line));
+ line->text = strbuf_detach(&sb, NULL);
+ if (p) {
+ line->next = p->next;
+ p->next = line;
+ } else {
+ line->next = NULL;
+ lines = line;
+ }
+ p = line;
+ }
+
+ lines = llist_mergesort(lines, get_next, set_next, compare_strings);
+
+ while (lines) {
+ printf("%s", lines->text);
+ lines = lines->next;
+ }
+ return 0;
+}