]> git.lizzy.rs Git - dragonstd.git/blob - test/test_queue.c
Use proper format for size_t printf
[dragonstd.git] / test / test_queue.c
1 #include <stdatomic.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include "../queue.h"
7
8 #define CHUNK_SIZE 1e5
9
10 Queue queue;
11
12 void *consumer(__attribute__((unused)) void *arg)
13 {
14         char *data;
15
16         while ((data = queue_deq(&queue, NULL)) != NULL) {
17                 // In a real world scenario, this would be something CPU intensive
18                 char c = 0;
19
20                 for (int i = 0; i < CHUNK_SIZE; i++)
21                         c += data[i];
22
23                 // How to convert char to int with zero-extend??? (MOVZX instruction on x86)
24                 // I literally have no idea why cast/coersion doesn't work, union seems to work.
25                 // But is it undefined behavior? And is there a better way to do it?
26                 // I can't access StackOverflow rn so I'm leaving it like this, for now.
27                 // I feel extremely stupid, feel free to make fun of me.
28                 // Also, some cute stuff I found: 376825 (You can thank me later シ)
29                 union {
30                         int i;
31                         char c;
32                 } u;
33                 u.i = 0;
34                 u.c = c;
35
36                 printf("%02x", u.i);
37                 fflush(stdout);
38
39                 free(data);
40         }
41
42         return NULL;
43 }
44
45 void *producer(__attribute__((unused)) void *arg)
46 {
47         // In a real world scenario, this would be something with actual latency,
48         // e.g. a network connection
49         // In this example, I'm creating the latency by allocating huge chunks of memory.
50         // Sometimes I kinda get the feeling I've fallen pretty low, but then I remember
51         // JavaScript programmers exist
52         // (DERZOMBIIIE IF YOU READ THIS IM SORRY NOTHING PERSONAL LUV U <3)
53         FILE *file = fopen("/dev/random", "r");
54         void *buffer;
55
56         do
57                 fread(buffer = malloc(CHUNK_SIZE), 1, CHUNK_SIZE, file);
58         while (queue_enq(&queue, buffer));
59
60         free(buffer);
61         fclose(file);
62
63         return NULL;
64 }
65
66 int main()
67 {
68         printf("-------------\n");
69         printf("Testing Queue\n");
70         printf("-------------\n");
71
72         printf("Random bullshit go: ");
73         queue_ini(&queue);
74
75         pthread_t threads[17];
76
77         for (int i = 0; i < 17; i++)
78                 pthread_create(&threads[i], NULL, i ? &consumer : &producer, NULL);
79
80         sleep(1);
81
82         queue_fin(&queue);
83         queue_cnl(&queue);
84
85         for (int i = 0; i < 17; i++)
86                 pthread_join(threads[i], NULL);
87
88         queue_dst(&queue);
89         printf("\n");
90 }
91
92 // Btw I figured out my MOVZX problem from earlier, but it's funny so I'm leaving it
93 // I even figured it out without StackOverflow pls tell me you're proud of me
94 // The reason why char -> int sometimes extends to 0 and sometimes to F is this:
95 // char is signed by default, so if the first bit (sign bit) is on, it will extend that
96 // to the first bit on the int (MOVSX instruction)
97 // just use unsigned char to avoid it
98
99 // ngl was even tempted to use inline assembly this is what happens to your brain when
100 // you work offline
101
102 // for real tho, why tf is char signed by default
103 // its probably machine dependent (since there is the explicit signed qualifier for char)
104
105 // This file is probably the biggest bogus I've ever published anyway