aboutsummaryrefslogtreecommitdiff
path: root/module-test/write_helper_test.cc
blob: eb86692ece40b57f675cfb0453349457cb6d9f8d (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

#include <iostream>
#include <print>
#include <unistd.h>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <print>

using namespace std;

size_t write_simulator(const char *s, size_t len, int &err)
{
    size_t rand_len = rand() % (len+1);
    size_t ret = write(STDOUT_FILENO, s, rand_len);
    err = errno;
    return ret;
}

int main()
{
    const char *s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz\n0123456789";
    srand(time(nullptr));
    size_t pos = 0;
    int err = EINTR;
    while (pos < strlen(s))
    {
        ;
        size_t ret = write_simulator(s+pos, strlen(s+pos), err);
        if (ret == string::npos) 
            if (err == EINTR) ret = 0;
            else return 0;
        pos += ret;
    }
    return 0;

}