summaryrefslogtreecommitdiff
path: root/tests/common/test_contracts.c
blob: 65eebcd0643014a1d21b87e6934df2db9e58aef2 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "common/backend.h"
#include "common/input.h"

#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

static void test_queue(void)
{
    struct input_queue q = {0};
    struct yasl_surface surface = {0};
    const enum yasl_input_kind kinds[] = {
        YASL_INPUT_TEXT, YASL_INPUT_PASTE, YASL_INPUT_DROP_FILES,
    };
    for (size_t k = 0; k < sizeof(kinds) / sizeof(kinds[0]); k++) {
        char *source = malloc(4);
        assert(source);
        memcpy(source, "a\0b", 4);
        struct yasl_input_event in = {.surface = &surface, .kind = kinds[k]};
        if (in.kind == YASL_INPUT_TEXT) { in.text.text = source; in.text.len = 3; }
        if (in.kind == YASL_INPUT_PASTE) { in.paste.text = source; in.paste.len = 3; }
        if (in.kind == YASL_INPUT_DROP_FILES) { in.drop_files.files = source; in.drop_files.len = 3; }
        assert(input_queue_push(&q, &in));
        memset(source, 'x', 4);
        free(source);
        struct yasl_input_event out;
        assert(input_queue_pop(&q, &out));
        const char *payload = out.kind == YASL_INPUT_TEXT ? out.text.text :
            out.kind == YASL_INPUT_PASTE ? out.paste.text : out.drop_files.files;
        assert(out.surface == &surface && out.kind == kinds[k]);
        assert(memcmp(payload, "a\0b", 4) == 0);
        assert(!input_queue_pop(&q, &out));
        assert(memcmp(payload, "a\0b", 4) == 0);
        /* Growth/wraparound must not invalidate a previously polled payload. */
        for (unsigned i = 0; i < 4096; i++) {
            in = (struct yasl_input_event){.kind = YASL_INPUT_KEY, .key.key = i};
            assert(input_queue_push(&q, &in));
        }
        assert(memcmp(payload, "a\0b", 4) == 0);
        for (unsigned i = 0; i < 2048; i++) {
            assert(input_queue_pop(&q, &out));
            assert(out.key.key == i);
        }
        for (unsigned i = 4096; i < 6144; i++) {
            in.key.key = i;
            assert(input_queue_push(&q, &in));
        }
        for (unsigned i = 2048; i < 6144; i++) {
            assert(input_queue_pop(&q, &out));
            assert(out.key.key == i);
        }
        assert(!input_queue_pop(&q, &out));
    }
    struct yasl_input_event empty = {.surface = &surface, .kind = YASL_INPUT_PASTE};
    struct yasl_input_event out;
    assert(input_queue_push(&q, &empty));
    assert(input_queue_pop(&q, &out));
    assert(out.paste.text && !out.paste.len && out.paste.text[0] == '\0');
    struct yasl_input_event in = {.surface = &surface, .kind = YASL_INPUT_TEXT,
                                 .text = {"owned", 5}};
    assert(input_queue_push(&q, &in));
    assert(input_queue_pop(&q, &out));
    const char *last = out.text.text;
    assert(input_queue_push(&q, &in));
    /* Exercise the allocation-failure reset path without allocating huge RAM. */
    in.text.len = SIZE_MAX;
    assert(!input_queue_push(&q, &in));
    assert(strcmp(last, "owned") == 0);
    in = (struct yasl_input_event){.surface = &surface, .kind = YASL_INPUT_KEY};
    assert(input_queue_push(&q, &in));
    assert(input_queue_pop(&q, &out) && out.kind == YASL_INPUT_RESET);
    assert(out.surface == &surface);
    assert(input_queue_pop(&q, &out) && out.kind == YASL_INPUT_KEY);
    assert(!input_queue_pop(&q, &out));
    in = (struct yasl_input_event){.kind = YASL_INPUT_PASTE, .paste = {"pending", 7}};
    assert(input_queue_push(&q, &in));
    input_queue_clear(&q);
    input_queue_clear(&q);
    assert(!q.events && !q.last_text && !q.count && !q.reset_pending);
}

static int presents, copies, pastes, creates;
static struct yasl_damage_list received;
static bool present(struct yasl_surface *s, const struct yasl_framebuffer *fb,
                    const struct yasl_damage_list *damage)
{
    (void)s; (void)fb;
    presents++;
    received = *damage;
    return true;
}
static uint32_t caps(const struct yasl_display *d) { (void)d; return 0; }
static void copy(struct yasl_display *d, enum yasl_selection sel, const char *text)
{ (void)d; (void)sel; (void)text; copies++; }
static void paste(struct yasl_display *d, enum yasl_selection sel, struct yasl_surface *s)
{ (void)d; (void)sel; (void)s; pastes++; }
static struct yasl_surface *create(struct yasl_display *d, const struct yasl_surface_spec *spec)
{ (void)d; (void)spec; creates++; return NULL; }

static void test_validation(void)
{
    const struct yasl_backend_ops ops = {.surface_present = present,
        .display_caps = caps, .display_copy = copy, .display_paste = paste,
        .surface_create_toplevel = create};
    struct yasl_display d = {.ops = &ops};
    struct yasl_surface s = {.display = &d, .frame_generation = 1};
    struct yasl_framebuffer fb = {.generation = 1, .size = {100, 80}};
    struct yasl_damage_list damage = {.n = -2};
    assert(!yasl_surface_present(&s, &fb, &damage));
    damage.n = YASL_DAMAGE_MAX + 1;
    assert(!yasl_surface_present(&s, &fb, &damage));
    damage.n = 1;
    damage.rects[0] = (struct yasl_rect2i){0, 0, -1, 1};
    assert(!yasl_surface_present(&s, &fb, &damage));
    assert(!yasl_surface_present(&s, &fb, NULL));
    assert(!presents);
    damage.n = -1;
    fb.generation = 0;
    assert(!yasl_surface_present(&s, &fb, &damage));
    fb.generation = 2;
    assert(!yasl_surface_present(&s, &fb, &damage));
    fb.generation = 1;
    damage.n = 4;
    damage.rects[0] = (struct yasl_rect2i){-10, -10, 30, 30};
    damage.rects[1] = (struct yasl_rect2i){90, 70, INT_MAX, INT_MAX};
    damage.rects[2] = (struct yasl_rect2i){INT_MAX, INT_MAX, INT_MAX, INT_MAX};
    damage.rects[3] = (struct yasl_rect2i){INT_MIN, INT_MIN, INT_MAX, INT_MAX};
    assert(yasl_surface_present(&s, &fb, &damage));
    assert(received.n == 2 && received.rects[0].x == 0 && received.rects[0].width == 20);
    assert(received.rects[1].width == 10 && received.rects[1].height == 10);
    assert(damage.n == 4 && damage.rects[0].x == -10);
    damage.n = -1;
    assert(yasl_surface_present(&s, &fb, &damage) && received.n == -1);
    damage.n = 0;
    assert(yasl_surface_present(&s, &fb, &damage) && received.n == 0);
    yasl_display_copy(&d, YASL_PRIMARY, "no alias");
    yasl_display_copy(&d, (enum yasl_selection)255, "invalid");
    yasl_display_paste(&d, YASL_PRIMARY, &s);
    yasl_display_paste(&d, (enum yasl_selection)255, &s);
    assert(!copies && !pastes);
    yasl_display_copy(&d, YASL_CLIPBOARD, "yes");
    yasl_display_paste(&d, YASL_CLIPBOARD, &s);
    assert(copies == 1 && pastes == 1);
    struct yasl_surface_spec spec = {.size = {100, 80}, .graphics_device = &d};
    assert(!yasl_surface_create_toplevel(&d, &spec) && errno == ENOTSUP);
    spec.graphics_device = NULL;
    spec.size.x = 0;
    assert(!yasl_surface_create_toplevel(&d, &spec) && errno == EINVAL);
    assert(!creates);
}

static void test_scale(void)
{
    assert(yasl_scale_to_device(-10, 120) == -10);
    assert(yasl_scale_to_device(-0.5, 120) == -1);
    assert(yasl_scale_to_device(0.5, 120) == 1);
    assert(yasl_scale_to_device(NAN, 120) == 0);
    assert(yasl_scale_to_device(INFINITY, 120) == 0);
    assert(yasl_scale_to_device(DBL_MAX, 240) == INT_MAX);
    assert(yasl_scale_to_logical(-1, 240) == -1);
    assert(yasl_scale_to_logical(1, 240) == 1);
    assert(yasl_scale_to_logical(INT_MAX, 1) == INT_MAX);
    assert(yasl_scale_to_logical(INT_MIN, 1) == INT_MIN);
    assert(yasl_scale_to_logical(1, 0) == 0);
}

int main(void)
{
    test_queue();
    test_validation();
    test_scale();
    return 0;
}