summaryrefslogtreecommitdiffstats
path: root/test/history-test.c
blob: 00468be0c2b8f808b90d706c9b31b8357bb30192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * rofi
 *
 * MIT/X11 License
 * Copyright © 2013-2017 Qball Cow <qball@gmpclient.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include <unistd.h>

#include <stdio.h>
#include <assert.h>
#include <glib.h>
#include <history.h>
#include <string.h>

static unsigned int test = 0;

#define TASSERT( a )    {                                \
        assert ( a );                                    \
        printf ( "Test %u passed (%s)\n", ++test, # a ); \
}

const char *file = "text";

static void history_test ( void )
{
    unlink ( file );

    // Empty list.
    unsigned int length = 0;
    char         **retv = history_get_list ( file, &length );

    TASSERT ( retv == NULL );
    TASSERT ( length == 0 );

    // 1 item
    history_set ( file, "aap" );

    retv = history_get_list ( file, &length );

    TASSERT ( retv != NULL );
    TASSERT ( length == 1 );
    TASSERT ( strcmp ( retv[0], "aap" ) == 0 );

    g_strfreev ( retv );

    // Remove entry
    history_remove ( file, "aap" );

    length = 0;
    retv   = history_get_list ( file, &length );

    TASSERT ( retv == NULL );
    TASSERT ( length == 0 );

    // 2 items
    history_set ( file, "aap" );
    history_set ( file, "aap" );

    retv = history_get_list ( file, &length );

    TASSERT ( retv != NULL );
    TASSERT ( length == 1 );

    TASSERT ( strcmp ( retv[0], "aap" ) == 0 );

    g_strfreev ( retv );

    for ( unsigned int in = length + 1; in < 26; in++ ) {
        char *p = g_strdup_printf ( "aap%u", in );
        printf ( "%s- %u\n", p, in );
        history_set ( file, p );
        retv = history_get_list ( file, &length );

        TASSERT ( retv != NULL );
        TASSERT ( length == ( in ) );

        g_strfreev ( retv );

        g_free ( p );
    }
    // Max 25 entries.
    history_set ( file, "blaat" );
    retv = history_get_list ( file, &length );

    TASSERT ( retv != NULL );
    TASSERT ( length == 25 );
    for ( unsigned int in = 0; in < 24; in++ ) {
        char *p = g_strdup_printf ( "aap%i", in + 2 );
        TASSERT ( g_strcmp0 ( retv[in], p ) == 0 );

        g_free ( p );
    }

    g_strfreev ( retv );

    unlink ( file );
}

int main (  G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv )
{
    history_test ();

    return 0;
}