libproff/list_test.c

/* [<][>]
[^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following functions.
  1. strl_new
  2. strl_free
  3. strl_mkstr
  4. main

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>

#define PLIST_ALLOC malloc

#include "list.h"

struct strl
{
    struct strl *next, *prev;
    char *data;
};

#define a(x) if(!(x)) {warnx("failed assertion(%s)", #x); fails++;}

static struct strl *
strl_new(char *data)
/* [<][>][^][v][top][bottom][index][help] */
{
    struct strl *sl = PLIST_NEW(struct strl);
    sl->data = strdup(data);
    return sl;
}

static void
strl_free(struct strl *sl)
/* [<][>][^][v][top][bottom][index][help] */
{
    free(sl->data);
    free(sl);
}

static char *
strl_mkstr(struct strl *sl)
/* [<][>][^][v][top][bottom][index][help] */
{
    struct strl *e;
    char *s;
    int n = 0;
    PLIST_FOREACH(sl, e)
        n+=strlen(e->data);
    s = calloc(1, n+1);
    PLIST_FOREACH(sl, e)
        strcat(s, e->data);
    return s;
}

static char *Koestler_fatigue_of_the_synapses = "\
I cannot authorise any altered version of my speech.\n\
It has to be transmitted according to the original text.\n\
I shall make you responsible for any deviation from it.\n\
\t-- Arthur Koestler";

int
main()
/* [<][>][^][v][top][bottom][index][help] */
{
    struct strl *sl = PLIST_NEW(struct strl),
                *sl2,
                *e;
    int fails = 0;
    char *k;
    PLIST_INIT(sl);
    a(PLIST_EMPTY(sl));
    PLIST_INSERT_HEAD(sl, strl_new("It has to be transmitted according"));
    PLIST_INSERT_TAIL(sl, (sl2=strl_new(" the original text.\n")));
    PLIST_INSERT_HEAD(sl, strl_new("altered version of my speech.\n"));
    PLIST_INSERT_TAIL(sl, strl_new("I shall make you responsible for any"));
    PLIST_INSERT_BEFORE(sl2, strl_new(" to"));
    PLIST_INSERT_BEFORE(PLIST_FIRST(sl), strl_new("I cannot authorise"));
    PLIST_INSERT_AFTER(PLIST_FIRST(sl), strl_new(" any "));
    PLIST_INSERT_AFTER(PLIST_LAST(sl), strl_new(" deviation from it.\n"));
    a(strcmp(PLIST_FIND(sl, sl2)->data, " the original text.\n") == 0);
    PLIST_INSERT_AFTER(sl2, strl_new(" Leontiev"));
    PLIST_DEL(PLIST_NEXT(sl2));
    PLIST_INSERT_TAIL(sl, strl_new("\t-- Arthur Koestler"));
    printf("Original:\n%s\n\n", Koestler_fatigue_of_the_synapses);
    printf("List:\n%s\n", (k=strl_mkstr(sl)));
    a(strcmp(k, Koestler_fatigue_of_the_synapses) == 0);
    PLIST_FOREACH_DEL(sl, e)
        strl_free(e);
    a(PLIST_EMPTY(sl));
    exit(fails>0);
}

/* [<][>][^][v][top][bottom][index][help] */