skip to content
==> tests/view.c <==
1/* peer.rs view_chunks_are_bounded_and_counted_once; LoadAvg's smoothing. */
2#include "mesh/check.h"
3#include "mesh/peer_view.h"
4#include "mesh/mt.h"
5
6#include <stdlib.h>
7
8int main(void) {
9    uint8_t ones[16], sevens[0x1000], nines[0x1000];
10    memset(ones, 1, sizeof ones), memset(sevens, 7, sizeof sevens), memset(nines, 9, sizeof nines);
11    char *err = NULL;
12    mpv_wait a = mpv_wait_new(1, 0, 0x2000);
13    CHECK(mpv_take(&a, UINT32_MAX, 0, ones, 1, &err) == -1 && mt_contains(err, "at most 8192"));
14    free(err);
15    CHECK(a.buf == NULL);
16    mpv_wait_free(&a);
17    mpv_wait b = mpv_wait_new(1, 0, 0x2000);
18    CHECK(mpv_take(&b, 0x2000, 0, sevens, 0x1000, &err) == 0);
19    CHECK(mpv_take(&b, 0x2000, 0, sevens, 0x1000, &err) == 0);
20    CHECK(mpv_take(&b, 0x2000, 0x1000, nines, 0x1000, &err) == 1);
21    CHECK(b.buf[0] == 7 && b.buf[0xfff] == 7 && b.buf[0x1000] == 9 && b.buf[0x1fff] == 9);
22    mpv_wait_free(&b);
23    mpv_wait c = mpv_wait_new(1, 0, 0x2000);
24    CHECK(mpv_take(&c, 0x2000, 0, ones, 16, &err) == 0);
25    CHECK(mpv_take(&c, 0x1000, 16, ones, 16, &err) == -1 && mt_contains(err, "disagree"));
26    free(err);
27    CHECK(mpv_take(&c, 0x2000, 0x1ff8, ones, 16, &err) == -1 && mt_contains(err, "past"));
28    free(err);
29    mpv_wait_free(&c);
30    mpv_wait d = mpv_wait_new(1, 0, 0x2000);
31    CHECK(mpv_take(&d, 0, 0, NULL, 0, &err) == 1);
32    mpv_wait_free(&d);
33    mpv_load l = {.at = 0, .cpu = 0.5f, .psi_cpu = mof32_some(0.1f), .share = mof32_none()};
34    mpv_load_add(&l, 1, 1.0f, mof32_some(0.6f), mof32_some(0.2f), mof32_some(0.4f));
35    CHECK(l.cpu == 0.5f + 0.2f * (1.0f - 0.5f) && l.psi_cpu.v == 0.1f + 0.2f * (0.6f - 0.1f) && l.psi_mem.v == 0.2f && l.share.v == 0.4f);
36    float before = l.cpu;
37    mpv_load_add(&l, 1, 0.0f, mof32_none(), mof32_none(), mof32_none());
38    CHECK(l.cpu == before && l.psi_cpu.some);
39    mpv_load_add(&l, 2, 0.0f, mof32_none(), mof32_none(), mof32_none());
40    CHECK(!l.psi_cpu.some && !l.share.some);
41    return mesh_check_done();
42}