-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratch.c
162 lines (125 loc) · 3.97 KB
/
scratch.c
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
/**
* scratch.c is intended to experiment with features of libraries to learn and
* experiment with other code, not to do unit tests on it.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <cairo.h>
#include "utils.h"
typedef int (*test_ptr)(void*);
int test_cairo_primitives(void *args);
int test_cairo_triangle(void *args);
int test_cairo_sierpinski_triangle(void *args);
int test_uncategorized(void *args);
int main (int argc, char *argv[]) {
test_ptr functions[] = {
test_cairo_primitives, test_cairo_triangle,
test_cairo_sierpinski_triangle, test_uncategorized
};
char *function_names[] = {
"test_cairo_primitives", "test_cairo_triangle",
"cairo_sierpinski_triangle", "test_uncategorized"
};
void *function_args[] = {
(void*) argv[1], (void*) argv[1], (void*) argv[1]
};
unsigned int functions_no = sizeof(functions) / sizeof(test_ptr);
int i, choice, exit_status;
printf(
"Which primitive do you want to test? [%d - %d]\n",
0, functions_no - 1
);
for (i = 0; i < functions_no; i++) {
printf("\t[%d] %s()\n", i, function_names[i]);
}
scanf("%d", &choice);
while (choice < 0 || choice >= functions_no) {
printf("It must be 0 <= choice < functions_no (choice = %d)\n", choice);
scanf("%d", &choice);
}
exit_status = functions[choice](function_args[choice]);
printf(
"%s() exited with status %d\n", function_names[choice], exit_status
);
return exit_status;
}
int test_cairo_primitives(void *args) {
char *filename = (char*) args;
const unsigned int width = 512, height = 512;
cairo_t *cr;
cairo_surface_t *surface;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cr = cairo_create(surface);
cairo_scale(cr, width, height);
cairo_set_source_rgb(cr, 256, 256, 256);
cairo_paint(cr);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 0.0025);
cairo_move_to(cr, 1.0 / 2, 1.0 / 5);
cairo_line_to(cr, 5.0 / 6, 4.0 / 5);
cairo_move_to(cr, 5.0 / 6, 4.0 / 5);
cairo_line_to(cr, 1.0 / 6, 4.0 / 5);
cairo_move_to(cr, 1.0 / 6, 4.0 / 5);
cairo_line_to(cr, 1.0 / 2, 1.0 / 5);
cairo_stroke(cr);
cairo_surface_write_to_png(surface, filename);
cairo_destroy(cr);
cairo_surface_destroy(surface);
return 0;
}
int test_cairo_triangle(void *args) {
const char *filename = (char*) args;
const unsigned int width = 512, height = 512;
point_t a = {1.0 / 6, 1.0 / 5}, b = {5.0 / 6, 1.0 / 5},
c = {1.0 / 2, 4.0 / 5.0};
cairo_t *cr;
cairo_surface_t *surface;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cr = cairo_create(surface);
cairo_scale(cr, width, height);
cairo_set_source_rgb(cr, 255, 255, 255);
cairo_paint(cr);
cairo_set_source_rgb(cr, 0, 172, 23);
cairo_set_line_width(cr, 0.0025);
cairo_triangle(cr, a, b, c);
cairo_stroke(cr);
cairo_surface_write_to_png(surface, filename);
cairo_destroy(cr);
cairo_surface_destroy(surface);
return 0;
}
int test_cairo_sierpinski_triangle(void *args) {
const char* filename = (char*) args;
const unsigned int width = 512, height = 512;
point_t a = {1.0 / 2, 1.0 / 5}, b = {5.0 / 6, 4.0 / 5},
c = {1.0 / 6, 4.0 / 5};
cairo_t *cr;
cairo_surface_t *surface;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cr = cairo_create(surface);
cairo_scale(cr, width, height);
cairo_set_source_rgb(cr, 255, 255, 255);
cairo_paint(cr);
cairo_set_source_rgb(cr, 75.0 / 256, 69.0 / 256, 209.0 / 256);
cairo_set_line_width(cr, 0.0025);
cairo_sierpinski_triangle(cr, a, b, c, 5);
cairo_stroke(cr);
cairo_surface_write_to_png(surface, filename);
cairo_destroy(cr);
cairo_surface_destroy(surface);
return 0;
}
int test_uncategorized(void *args) {
char *digits = "0123456ABCDEFZXYWVUwvu";
char *numbers[] = {"A494BC", "AA", "AF", "A0", "C0"};
int i, s = 5;
for (i = 0; i < strlen(digits); i++) {
printf("isxdigit(%c) == %d\n", digits[i], isxdigit(digits[i]));
}
for (i = 0; i < s; i++) {
printf("strtol(\"%s\") = %ld\n", numbers[i], strtol(numbers[i], NULL, 16));
}
return 0;
}