├── stupidity.c ├── mockmoulinette ├── evaluateuser ├── config_d06.pl ├── config_deval.pl ├── README.md ├── config_d03.pl ├── config_d02.pl ├── config_d10.pl ├── config_d07.pl ├── config_d08.pl ├── config_d04.pl ├── config_dmatch.pl ├── config_d05.pl ├── spawn.pl └── config_d11.pl /stupidity.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int ft_putchar(char c); 5 | 6 | int ft_putchar(char c) 7 | { 8 | return write(STDOUT_FILENO, &c, 1); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /mockmoulinette: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git clone https://github.com/kristofk/MockMoulinette.git MockMoulinette -q 3 | cd MockMoulinette 4 | sh evaluateuser $1 $2 5 | cd .. 6 | rm -rf MockMoulinette 7 | -------------------------------------------------------------------------------- /evaluateuser: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo "EVALUATING USER" 3 | git clone $1 userproject -q 4 | clear 5 | ./spawn.pl userproject "config_d$2.pl" 6 | clear 7 | ./tools/build.sh 8 | clear 9 | 10 | echo "+-+-+-+-+-+-+-+-+-+-+" >> RESULT.txt 11 | echo "|N|o|r|m|i|n|e|t|t|e|" >> RESULT.txt 12 | echo "+-+-+-+-+-+-+-+-+-+-+\n" >> RESULT.txt 13 | 14 | ./tools/verify.sh >> RESULT.txt 15 | 16 | echo "\n+-+-+-+-+-+-+-+-+-+-+" >> RESULT.txt 17 | echo "|M|o|u|l|i|n|e|t|t|e|" >> RESULT.txt 18 | echo "+-+-+-+-+-+-+-+-+-+-+\n" >> RESULT.txt 19 | 20 | ./tools/check_all.sh >> RESULT.txt 21 | 22 | cp RESULT.txt ~/Desktop 23 | 24 | echo "The results can be found on the Desktop in RESULT.txt" 25 | open ~/Desktop/RESULT.txt 26 | -------------------------------------------------------------------------------- /config_d06.pl: -------------------------------------------------------------------------------- 1 | 2 | ex01 -p 3 | ft_print_program_name 4 | check -t ==== 5 | %tests = ( 6 | "$program" => "$program\n", 7 | "$program a b c" => "$program\n", 8 | ); 9 | ==== 10 | 11 | 12 | ex02 -p 13 | ft_print_params 14 | check -t ==== 15 | %tests = ( 16 | "$program" => "", 17 | "$program a" => "a\n", 18 | "$program a b" => "a\nb\n", 19 | "$program asdf beta char" => "asdf\nbeta\nchar\n", 20 | "$program 'hello world' !" => "hello world\n!\n", 21 | "$program '' lol" => "\nlol\n", 22 | ); 23 | ==== 24 | 25 | 26 | ex03 -p 27 | ft_rev_params 28 | check -t ==== 29 | %tests = ( 30 | "$program" => "", 31 | "$program a" => "a\n", 32 | "$program a b" => "b\na\n", 33 | "$program asdf beta char" => "char\nbeta\nasdf\n", 34 | "$program 'lol hi' test" => "test\nlol hi\n", 35 | "$program '' !" => "!\n\n", 36 | ); 37 | ==== 38 | 39 | 40 | ex04 -p 41 | ft_sort_params 42 | check -t ==== 43 | %tests = ( 44 | "$program" => "", 45 | "$program asdf beta char" => "asdf\nbeta\nchar\n", 46 | "$program abcd abc ab a" => "a\nab\nabc\nabcd\n", 47 | "$program zxcv '' qwer 1234 asdf" => "\n1234\nasdf\nqwer\nzxcv\n", 48 | ); 49 | ==== 50 | 51 | -------------------------------------------------------------------------------- /config_deval.pl: -------------------------------------------------------------------------------- 1 | 2 | ex00 -N -f=Makefile -F=*.[ch] 3 | eval_expr 4 | check -m=eval_expr -t ==== 5 | %tests = ( 6 | "$program" => '', 7 | "$program '5 + 5'" => "10\n", 8 | "$program '8 - 5'" => "3\n", 9 | "$program '5 - 20 - 15'" => "-30\n", 10 | "$program '5 * 5'" => "25\n", 11 | 12 | "$program '25 / 6'" => "4\n", 13 | "$program '25 % 6'" => "1\n", 14 | "$program '24 % 6'" => "0\n", 15 | "$program '27 % 6'" => "3\n", 16 | 17 | "$program '5 + 4 + 3'" => "12\n", 18 | "$program '5 + 4 * 3'" => "17\n", 19 | "$program '5 * 4 * 3'" => "60\n", 20 | "$program '5 * 4 + 3'" => "23\n", 21 | 22 | "$program '5 / 4 * 4'" => "4\n", 23 | "$program '5 * 4 / 4'" => "5\n", 24 | "$program '5 % 4 * 4'" => "4\n", 25 | "$program '5 * 4 % 3'" => "2\n", 26 | "$program '5 % 4 % 3'" => "1\n", 27 | 28 | "$program '(5 + 4)'" => "9\n", 29 | "$program '(5 - 4)'" => "1\n", 30 | "$program '(5 * 4)'" => "20\n", 31 | "$program '(10 / 4)'" => "2\n", 32 | "$program '(5 % 4)'" => "1\n", 33 | 34 | "$program '5 + (5 + 4)'" => "14\n", 35 | "$program '5 - (5 - 4)'" => "4\n", 36 | "$program '5 * (5 * 4)'" => "100\n", 37 | "$program '5 / (10 / 4)'" => "2\n", 38 | "$program '5 % (5 % 4)'" => "0\n", 39 | 40 | "$program '5 + (3 + 5) * 2'" => "21\n", 41 | "$program '5 - (10 - 4) / 3'" => "3\n", 42 | "$program '5 * (5 + 3) - 4'" => "36\n", 43 | "$program '10 / (10 / 4) / 4'" => "1\n", 44 | "$program '5 % (5 % 3) % 5'" => "1\n", 45 | 46 | "$program '(((5 * 7)))'" => "35\n", 47 | "$program '(5 + ((3 * 4) - 2))'" => "15\n", 48 | ); 49 | ==== 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MockMoulinette 2 | 3 | A script that runs Norminette and compiles your code and runs some Moulinette checks automatically with the correct flags. Great for peer reviews and checking your own work. 4 | 5 | The community's best attempt to recreate Moulinette. 6 | 7 | It isn't 100% accourate. So if it says everything is good, it might still be wrong. Also, it might have some false-positive errors. But again, this is the best we got. 8 | 9 | If you notice an error please open an [issue](https://github.com/kristofk/MockMoulinette/issues) about it here in the repo. 10 | 11 | The Moulinette checks are based on [42us-stupidity](https://github.com/mirror12k/42us-stupidity). 12 | 13 | ## Prerequisits 14 | 15 | You have to run this on a computer provided by School42. There are 2 reasons for this: 16 | 17 | - Only those machines have the necessery kerberos tickets and access rights 18 | - Only those machines have the command necessary for this project: `norminette` 19 | 20 | ## Usage 21 | 22 | 1. Go to [releases](https://github.com/kristofk/MockMoulinette/releases) 23 | 2. From the latest release download `mockmoulinette` 24 | 3. Go into the download folder and run: 25 | 26 | > `sh mockmoulinette ` 27 | 28 | (e.g. `sh mockmoulinette vogsphere@vgs.42.us.org:intra/2019/activities/piscine_c_day_06/uuser 06`) 29 | 30 | Now you should have the results on the Desktop in RESULT.txt file and this file should be open infront of you. 31 | 32 | 4. Before you run it again delete the RESULT.txt file from the Desktop. 33 | 34 | ## Common errors and mistakes 35 | 36 | **Error: No such file or directory** 37 | - Solution #1: Check the extension, some browsers put some extension to the file 38 | - Solution #2: Check that you are in the right folder: the one that contains the script 39 | 40 | **Other errors** 41 | - Solution #1: Make sure that the arguments are correct in the script call (`sh mockmoulinette `) 42 | - Solution #2: Open an [issue](https://github.com/kristofk/MockMoulinette/issues) 43 | 44 | ## Contributing 45 | 46 | If you notice a bug or an error or have a question then open an [issue](https://github.com/kristofk/MockMoulinette/issues) regarding it. 47 | 48 | If you want to help but don't know how then check the [issues](https://github.com/kristofk/MockMoulinette/issues). 49 | 50 | When you have changes ready, you can create pull requests. 51 | 52 | I will respond to everyone asap. 53 | 54 | ## Example outputs 55 | 56 | Example of a perfect output: 57 | ``` 58 | +-+-+-+-+-+-+-+-+-+-+ 59 | |N|o|r|m|i|n|e|t|t|e| 60 | +-+-+-+-+-+-+-+-+-+-+ 61 | 62 | Norme: ./work/ex00/ft_ft.c 63 | Norme: ./work/ex01/ft_ultimate_ft.c 64 | Norme: ./work/ex02/ft_swap.c 65 | Norme: ./work/ex03/ft_div_mod.c 66 | Norme: ./work/ex04/ft_ultimate_div_mod.c 67 | Norme: ./work/ex05/ft_putstr.c 68 | Norme: ./work/ex06/ft_strlen.c 69 | Norme: ./work/ex07/ft_strrev.c 70 | Norme: ./work/ex08/ft_atoi.c 71 | Norme: ./work/ex09/ft_sort_integer_table.c 72 | 73 | +-+-+-+-+-+-+-+-+-+-+ 74 | |M|o|u|l|i|n|e|t|t|e| 75 | +-+-+-+-+-+-+-+-+-+-+ 76 | 77 | work/ex00/main good! 78 | work/ex01/main good! 79 | work/ex02/main good! 80 | work/ex03/main good! 81 | work/ex04/main good! 82 | work/ex05/main_basic good! 83 | work/ex05/main_multiple good! 84 | work/ex05/main_empty good! 85 | work/ex06/main_basic good! 86 | work/ex06/main_empty good! 87 | work/ex07/main_basic good! 88 | work/ex07/main_empty good! 89 | work/ex08/main_basic good! 90 | work/ex08/main_big good! 91 | work/ex09/main_basic good! 92 | ``` 93 | -------------------------------------------------------------------------------- /config_d03.pl: -------------------------------------------------------------------------------- 1 | ex00 2 | ft_ft(int *nbr) 3 | main ==== 4 | int main() 5 | { 6 | int v = 15; 7 | ft_ft(&v); 8 | printf("%d", v); 9 | return 0; 10 | } 11 | ==== 12 | check -e ==== 13 | $expected = '42'; 14 | ==== 15 | 16 | 17 | ex01 18 | ft_ultimate_ft(int *********nbr) 19 | main ==== 20 | int main() 21 | { 22 | int a = 15; 23 | int* b = &a; 24 | int** c = &b; 25 | int*** d = &c; 26 | int**** e = &d; 27 | int***** f = &e; 28 | int****** g = &f; 29 | int******* h = &g; 30 | int******** i = &h; 31 | int********* j = &i; 32 | 33 | ft_ultimate_ft(j); 34 | 35 | printf("%d", a); 36 | 37 | return 0; 38 | } 39 | ==== 40 | check -e ==== 41 | $expected = '42'; 42 | ==== 43 | 44 | 45 | ex02 46 | ft_swap(int *a, int *b) 47 | main ==== 48 | int main() 49 | { 50 | int a = 15; 51 | int b = -25; 52 | ft_swap(&a, &b); 53 | printf("%d,%d", a, b); 54 | return 0; 55 | } 56 | ==== 57 | check -e ==== 58 | $expected = '-25,15'; 59 | ==== 60 | 61 | 62 | ex03 63 | ft_div_mod(int a, int b, int *div, int *mod) 64 | main ==== 65 | int main() 66 | { 67 | int div = 0, mod = 0; 68 | ft_div_mod(40, 15, &div, &mod); 69 | printf("%d,%d", div, mod); 70 | return 0; 71 | } 72 | ==== 73 | check -e ==== 74 | $expected = '2,10'; 75 | ==== 76 | 77 | 78 | ex04 79 | ft_ultimate_div_mod(int *a, int *b) 80 | main ==== 81 | int main() 82 | { 83 | int a = 40, b = 15; 84 | ft_ultimate_div_mod(&a, &b); 85 | printf("%d,%d", a, b); 86 | return 0; 87 | } 88 | ==== 89 | check -e ==== 90 | $expected = '2,10'; 91 | ==== 92 | 93 | 94 | ex05 95 | ft_putstr(char *str) 96 | main_basic ==== 97 | int main() 98 | { 99 | ft_putstr("hello world!"); 100 | return 0; 101 | } 102 | ==== 103 | check_basic -e ==== 104 | $expected = 'hello world!'; 105 | ==== 106 | main_multiple ==== 107 | int main() 108 | { 109 | ft_putstr("test1\n"); 110 | ft_putstr("test2\n"); 111 | ft_putstr("test3\n"); 112 | return 0; 113 | } 114 | ==== 115 | check_multiple -e ==== 116 | $expected = "test1\ntest2\ntest3\n"; 117 | ==== 118 | main_empty ==== 119 | int main () 120 | { 121 | ft_putstr(""); 122 | ft_putstr(""); 123 | ft_putstr(""); 124 | return 0; 125 | } 126 | ==== 127 | check_empty -e ==== 128 | $expected = ''; 129 | ==== 130 | 131 | 132 | ex06 133 | int ft_strlen(char *str) 134 | main_basic ==== 135 | int main() 136 | { 137 | printf("%d,%d,%d", ft_strlen("asdf"), ft_strlen("qwerty"), ft_strlen("zxc0zxc")); 138 | return 0; 139 | } 140 | ==== 141 | check_basic -e ==== 142 | $expected = '4,6,7'; 143 | ==== 144 | main_empty ==== 145 | int main() 146 | { 147 | printf("%d", ft_strlen("")); 148 | return 0; 149 | } 150 | ==== 151 | check_empty -e ==== 152 | $expected = '0'; 153 | ==== 154 | 155 | 156 | ex07 157 | char* ft_strrev(char *str) 158 | main_basic ==== 159 | int main () 160 | { 161 | char test1[] = "asdf"; 162 | printf("%s\n", ft_strrev(test1)); 163 | char test2[] = "qwert"; 164 | printf("%s\n", ft_strrev(test2)); 165 | char test3[] = "z"; 166 | printf("%s\n", ft_strrev(test3)); 167 | char test4[] = "bc"; 168 | printf("%s\n", ft_strrev(test4)); 169 | } 170 | ==== 171 | check_basic -e ==== 172 | $expected = "fdsa\ntrewq\nz\ncb\n"; 173 | ==== 174 | main_empty ==== 175 | int main () 176 | { 177 | char test[] = ""; 178 | printf("%s", ft_strrev(test)); 179 | } 180 | ==== 181 | check_empty -e ==== 182 | $expected = ''; 183 | ==== 184 | 185 | 186 | ex08 187 | int ft_atoi(char *str) 188 | main_basic ==== 189 | int main() 190 | { 191 | printf("%d,%d,%d", ft_atoi("15"), ft_atoi("0"), ft_atoi("-25")); 192 | return 0; 193 | } 194 | ==== 195 | check_basic -e ==== 196 | $expected = '15,0,-25'; 197 | ==== 198 | main_big ==== 199 | int main() 200 | { 201 | printf("%d,%d", ft_atoi("2147483647"), ft_atoi("-2147483648")); 202 | } 203 | ==== 204 | check_big -e ==== 205 | $expected = '2147483647,-2147483648'; 206 | ==== 207 | 208 | 209 | ex09 210 | ft_sort_integer_table(int *tab, int size) 211 | main_basic ==== 212 | int main() 213 | { 214 | int test1[] = {1,5,4,2,3}; 215 | int size = 5; 216 | ft_sort_integer_table(test1, size); 217 | 218 | for (int i = 0; i < size; i++) 219 | printf("%d,", test1[i]); 220 | } 221 | ==== 222 | check_basic -e ==== 223 | $expected = join '', map "$_,", 1 .. 5; 224 | ==== 225 | -------------------------------------------------------------------------------- /config_d02.pl: -------------------------------------------------------------------------------- 1 | ex00 2 | ft_print_alphabet 3 | main ==== 4 | int main() 5 | { 6 | ft_print_alphabet(); 7 | } 8 | ==== 9 | check -e ==== 10 | $expected = join '', 'a' .. 'z'; 11 | ==== 12 | 13 | 14 | 15 | ex01 16 | ft_print_reverse_alphabet 17 | main ==== 18 | int main() 19 | { 20 | ft_print_reverse_alphabet(); 21 | } 22 | ==== 23 | check -e ==== 24 | $expected = join '', reverse 'a' .. 'z'; 25 | ==== 26 | 27 | 28 | 29 | ex02 30 | ft_print_numbers 31 | main ==== 32 | int main() 33 | { 34 | ft_print_numbers(); 35 | } 36 | ==== 37 | check -e ==== 38 | $expected = join '', '0' .. '9'; 39 | ==== 40 | 41 | 42 | 43 | ex03 44 | ft_is_negative(int n) 45 | main_basic ==== 46 | int main() 47 | { 48 | ft_is_negative(15); 49 | ft_is_negative(-25); 50 | ft_is_negative(12345678); 51 | } 52 | ==== 53 | check_basic -e ==== 54 | $expected = 'PNP'; 55 | ==== 56 | main_zero ==== 57 | int main () 58 | { 59 | ft_is_negative(0); 60 | } 61 | ==== 62 | check_zero -e ==== 63 | $expected = 'P'; 64 | ==== 65 | 66 | 67 | 68 | ex04 69 | ft_print_comb 70 | main ==== 71 | int main() 72 | { 73 | ft_print_comb(); 74 | } 75 | ==== 76 | check -e ==== 77 | $expected = join ', ', 78 | grep { 79 | my ($a, $b, $c) = split '', $_; 80 | $a < $b and $b < $c; 81 | } 82 | '000' .. '999'; 83 | ==== 84 | main_multiple ==== 85 | int main () 86 | { 87 | ft_print_comb(); 88 | ft_print_comb(); 89 | } 90 | ==== 91 | check_multiple -e ==== 92 | $expected = join ', ', 93 | grep { 94 | my ($a, $b, $c) = split '', $_; 95 | $a < $b and $b < $c; 96 | } 97 | '000' .. '999'; 98 | $expected = "$expected$expected"; 99 | ==== 100 | 101 | 102 | 103 | ex05 104 | ft_print_comb2 105 | main ==== 106 | int main () 107 | { 108 | ft_print_comb2(); 109 | } 110 | ==== 111 | check -e ==== 112 | $expected = join ', ', 113 | map { "$_->[0] $_->[1]" } 114 | grep { $_->[0] < $_->[1] } 115 | map { 116 | my $first = $_; 117 | map { [$first, $_] } '00' .. '99' 118 | } 119 | '00' .. '99'; 120 | ==== 121 | main_multiple ==== 122 | int main () 123 | { 124 | ft_print_comb2(); 125 | ft_print_comb2(); 126 | } 127 | ==== 128 | check_multiple -e ==== 129 | $expected = join ', ', 130 | map { "$_->[0] $_->[1]" } 131 | grep { $_->[0] < $_->[1] } 132 | map { 133 | my $first = $_; 134 | map { [$first, $_] } '00' .. '99' 135 | } 136 | '00' .. '99'; 137 | $expected = "$expected$expected"; 138 | ==== 139 | 140 | 141 | 142 | ex06 143 | ft_putnbr(int nb) 144 | main_basic ==== 145 | int main () 146 | { 147 | ft_putnbr(15); 148 | } 149 | ==== 150 | check_basic -e ==== 151 | $expected = '15'; 152 | ==== 153 | main_negative ==== 154 | int main () 155 | { 156 | ft_putnbr(-25); 157 | } 158 | ==== 159 | check_negative -e ==== 160 | $expected = '-25'; 161 | ==== 162 | main_zero ==== 163 | int main () 164 | { 165 | ft_putnbr(0); 166 | } 167 | ==== 168 | check_zero -e ==== 169 | $expected = '0'; 170 | ==== 171 | main_intmax ==== 172 | int main () 173 | { 174 | ft_putnbr(2147483647); 175 | } 176 | ==== 177 | check_intmax -e ==== 178 | $expected = '2147483647'; 179 | ==== 180 | main_nintmax ==== 181 | int main () 182 | { 183 | ft_putnbr(-2147483648); 184 | } 185 | ==== 186 | check_nintmax -e ==== 187 | $expected = -2147483648; 188 | ==== 189 | 190 | 191 | 192 | ex07 193 | ft_print_combn(int n) 194 | main_comb1 ==== 195 | int main () 196 | { 197 | ft_print_combn(1); 198 | } 199 | ==== 200 | check_comb1 -e ==== 201 | $expected = join ', ', '0' .. '9'; 202 | ==== 203 | main_comb2 ==== 204 | int main () 205 | { 206 | ft_print_combn(2); 207 | } 208 | ==== 209 | check_comb2 -e ==== 210 | $expected = join ', ', 211 | grep { 212 | my ($a, $b) = split '', $_; 213 | $a < $b 214 | } 215 | '00' .. '99'; 216 | ==== 217 | main_comb3 ==== 218 | int main () 219 | { 220 | ft_print_combn(3); 221 | } 222 | ==== 223 | check_comb3 -e ==== 224 | $expected = join ', ', 225 | grep { 226 | my ($a, $b, $c) = split '', $_; 227 | $a < $b and $b < $c 228 | } 229 | '000' .. '999'; 230 | ==== 231 | main_comb_multiple ==== 232 | int main () 233 | { 234 | ft_print_combn(1); 235 | ft_print_combn(2); 236 | ft_print_combn(1); 237 | } 238 | ==== 239 | check_comb_multiple -e ==== 240 | $expected = join ', ', '0' .. '9'; 241 | $expected .= join ', ', 242 | grep { 243 | my ($a, $b) = split '', $_; 244 | $a < $b 245 | } 246 | '00' .. '99'; 247 | $expected .= join ', ', '0' .. '9'; 248 | ==== 249 | -------------------------------------------------------------------------------- /config_d10.pl: -------------------------------------------------------------------------------- 1 | 2 | 3 | ex01 4 | ft_foreach(int* tab, int length, void (*f)(int)) 5 | main ==== 6 | 7 | void putnbr(int n) 8 | { 9 | printf("%d,", n); 10 | } 11 | 12 | int main() 13 | { 14 | int test1[] = {1,2,3,4,5}; 15 | int test2[] = {-25,0,20,45}; 16 | int test3[] = {5}; 17 | 18 | ft_foreach(test1, 5, putnbr); 19 | printf("\n"); 20 | ft_foreach(test2, 3, putnbr); 21 | printf("\n"); 22 | ft_foreach(test3, 0, putnbr); 23 | printf("\n"); 24 | } 25 | 26 | 27 | ==== check -e ==== 28 | $expected = "1,2,3,4,5,\n-25,0,20,\n\n"; 29 | ==== 30 | 31 | 32 | ex02 33 | int* ft_map(int* tab, int length, int(*f)(int)) 34 | main ==== 35 | 36 | int inc_num(int n) 37 | { 38 | return n + 5; 39 | } 40 | 41 | int main() 42 | { 43 | int test1[] = {1,2,3,4,5}; 44 | 45 | int* res = ft_map(test1, 5, inc_num); 46 | for (int i = 0; i < 5; i++) 47 | printf("%d,", res[i]); 48 | printf("\n"); 49 | 50 | free(res); 51 | 52 | int test2[] = {-25,0,20,45}; 53 | 54 | res = ft_map(test2, 3, inc_num); 55 | for (int i = 0; i < 3; i++) 56 | printf("%d,", res[i]); 57 | printf("\n"); 58 | 59 | free(res); 60 | 61 | int test3[] = {5}; 62 | 63 | res = ft_map(test3, 0, inc_num); 64 | for (int i = 0; i < 0; i++) 65 | printf("%d,", res[i]); 66 | printf("\n"); 67 | 68 | free(res); 69 | 70 | } 71 | ==== check -e ==== 72 | $expected = "6,7,8,9,10,\n-20,5,25,\n\n"; 73 | ==== 74 | 75 | 76 | ex03 77 | int ft_any(char** tab, int (*f)(char*)) 78 | main ==== 79 | 80 | int starts_with_z(char* str) 81 | { 82 | return str[0] == 'z'; 83 | } 84 | 85 | int main(int argc, char** argv) 86 | { 87 | printf("%d", ft_any(argv + 1, starts_with_z)); 88 | if (argc < 2) 89 | return 1; 90 | } 91 | ==== check -t ==== 92 | %tests = ( 93 | "$program" => 0, 94 | "$program asdf" => 0, 95 | "$program z" => 1, 96 | "$program asdf zxcv" => 1, 97 | "$program asdf qwer zxcv uiop hjkl" => 1, 98 | "$program asdf qwer xcv uiop hjkl" => 0, 99 | "$program '' '' z '' ''" => 1, 100 | "$program '' '' a '' ''" => 0, 101 | "$program zxcv asdf" => 1, 102 | "$program zxcv asdf z zz zxcv" => 1, 103 | "$program a a a a a a a a a a a a a a a" => 0, 104 | ); 105 | ==== 106 | 107 | 108 | ex04 109 | int ft_count_if(char** tab, int (*f)(char*)) 110 | main ==== 111 | 112 | int starts_with_z(char* str) 113 | { 114 | return str[0] == 'z'; 115 | } 116 | 117 | int main(int argc, char** argv) 118 | { 119 | printf("%d", ft_count_if(argv + 1, starts_with_z)); 120 | if (argc < 2) 121 | return 0; 122 | } 123 | ==== check -t ==== 124 | %tests = ( 125 | "$program" => 0, 126 | "$program asdf" => 0, 127 | "$program z" => 1, 128 | "$program asdf zxcv" => 1, 129 | "$program asdf qwer zxcv uiop hjkl" => 1, 130 | "$program asdf qwer xcv uiop hjkl" => 0, 131 | "$program '' '' z '' ''" => 1, 132 | "$program '' '' a '' ''" => 0, 133 | "$program zxcv asdf" => 1, 134 | "$program zxcv asdf zxcv" => 2, 135 | "$program z z z z z" => 5, 136 | "$program asdf qwer io uiop zxcxcvzzzzzz" => 1, 137 | ); 138 | ==== 139 | 140 | 141 | ex05 142 | int ft_is_sort(int* tab, int length, int (*f)(int, int)) 143 | main ==== 144 | 145 | int cmpnbr(int a, int b) 146 | { 147 | return a - b; 148 | } 149 | 150 | int main(int argc, char** argv) 151 | { 152 | if (argc < 1) 153 | return 0; 154 | int test[argc - 1]; 155 | for (int i = 1; i < argc; i++) 156 | test[i - 1] = atoi(argv[i]); 157 | 158 | printf("%d", ft_is_sort(test, argc - 1, cmpnbr)); 159 | } 160 | ==== check -t ==== 161 | %tests = ( 162 | "$program" => 1, 163 | "$program 5" => 1, 164 | "$program 1 5" => 1, 165 | "$program 5 1" => 0, 166 | "$program -1 5" => 1, 167 | "$program 1 -5" => 0, 168 | "$program 1 1" => 1, 169 | "$program 1 2 3 4 5" => 1, 170 | "$program 1 2 3 4 5 6 7 8 9" => 1, 171 | "$program 1 2 3 4 5 6 7 8 7" => 0, 172 | "$program 1 2 3 4 5 8 7 8 9" => 0, 173 | "$program 3 2 3 4 5 6 7 8 9" => 0, 174 | ); 175 | ==== 176 | 177 | 178 | ex07 179 | ft_sort_wordtab(char** tab) 180 | main ==== 181 | int main(int argc, char** argv) 182 | { 183 | if (argc < 1) 184 | return 0; 185 | ft_sort_wordtab(argv + 1); 186 | for (char** iter = argv + 1; *iter != 0; iter++) 187 | printf("%s,", *iter); 188 | } 189 | ==== check -t ==== 190 | %tests = ( 191 | "$program" => '', 192 | "$program a" => 'a,', 193 | "$program c b a" => 'a,b,c,', 194 | "$program b c a" => 'a,b,c,', 195 | "$program a c b" => 'a,b,c,', 196 | "$program c b a d" => 'a,b,c,d,', 197 | "$program a b" => 'a,b,', 198 | "$program b a" => 'a,b,', 199 | "$program asdf asde asdd" => 'asdd,asde,asdf,', 200 | ); 201 | ==== 202 | 203 | 204 | 205 | 206 | ex08 207 | ft_advanced_sort_wordtab(char** tab, int(*cmp)(char*, char*)) 208 | main ==== 209 | 210 | int my_reverse_strcmp(char* s1, char* s2) 211 | { 212 | while (*s1 == *s2 && *s1 != 0) 213 | { 214 | s1++; 215 | s2++; 216 | } 217 | return *s2 - *s1; 218 | } 219 | 220 | int main(int argc, char** argv) 221 | { 222 | if (argc < 1) 223 | return 1; 224 | ft_advanced_sort_wordtab(argv + 1, my_reverse_strcmp); 225 | 226 | for (char** iter = argv + 1; *iter != 0; iter++) 227 | printf("%s,", *iter); 228 | } 229 | ==== check -t ==== 230 | %tests = ( 231 | "$program" => '', 232 | "$program a" => 'a,', 233 | "$program c b a" => 'c,b,a,', 234 | "$program b c a" => 'c,b,a,', 235 | "$program a c b" => 'c,b,a,', 236 | "$program c b a d" => 'd,c,b,a,', 237 | "$program a b" => 'b,a,', 238 | "$program b a" => 'b,a,', 239 | "$program asdf asde asdd" => 'asdf,asde,asdd,', 240 | ); 241 | ==== 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | -------------------------------------------------------------------------------- /config_d07.pl: -------------------------------------------------------------------------------- 1 | ex00 2 | char* ft_strdup(char* src) 3 | main -m ==== 4 | char* str; 5 | char* res; 6 | str = "asdf", res = ft_strdup(str); 7 | printf("%d, '%s' vs '%s'\n", res != str, res, str); 8 | str = "", res = ft_strdup(str); 9 | printf("%d, '%s' vs '%s'\n", res != str, res, str); 10 | str = "hello world!", res = ft_strdup(str); 11 | printf("%d, '%s' vs '%s'\n", res != str, res, str); 12 | ==== check -e ==== 13 | $expected = "1, 'asdf' vs 'asdf' 14 | 1, '' vs '' 15 | 1, 'hello world!' vs 'hello world!' 16 | "; 17 | ==== 18 | 19 | 20 | ex01 21 | int* ft_range(int min, int max) 22 | main -m ==== 23 | #include 24 | #include 25 | int* res; 26 | int i; 27 | res = ft_range(5, 10); 28 | for (i = 0; i < 5; i++) 29 | printf("%d,", res[i]); 30 | printf("\n"); 31 | 32 | res = ft_range(-20, -15); 33 | for (i = 0; i < 5; i++) 34 | printf("%d,", res[i]); 35 | printf("\n"); 36 | 37 | res = ft_range(100, 101); 38 | for (i = 0; i < 1; i++) 39 | printf("%d,", res[i]); 40 | printf("\n"); 41 | 42 | res = ft_range(10, 5); 43 | printf("%" PRIxPTR "\n", (uintptr_t) res); 44 | ==== check -e ==== 45 | $expected = '5,6,7,8,9, 46 | -20,-19,-18,-17,-16, 47 | 100, 48 | 0 49 | '; 50 | ==== 51 | 52 | 53 | ex02 54 | int ft_ultimate_range(int **range, int min, int max) 55 | main -m ==== 56 | int* res = NULL; 57 | int i; 58 | int size; 59 | 60 | size = ft_ultimate_range(&res, 5, 10); 61 | printf("is_null? %d\n", res == NULL); 62 | printf("size is %i\n", size); 63 | for (i = 0; i < 5; i++) 64 | printf("%d,", res[i]); 65 | printf("\n"); 66 | 67 | res = NULL; 68 | size = ft_ultimate_range(&res, -20, -17); 69 | printf("is_null? %d\n", res == NULL); 70 | printf("size is %i\n", size); 71 | for (i = 0; i < 3; i++) 72 | printf("%d,", res[i]); 73 | printf("\n"); 74 | 75 | res = (int*)1; 76 | size = ft_ultimate_range(&res, 10, 5); 77 | printf("is_null? %d\n", res == NULL); 78 | printf("size is %i\n", size); 79 | ==== check -e ==== 80 | $expected = 'is_null? 0 81 | size is 5 82 | 5,6,7,8,9, 83 | is_null? 0 84 | size is 3 85 | -20,-19,-18, 86 | is_null? 1 87 | size is 0 88 | '; 89 | ==== 90 | 91 | 92 | ex03 93 | char* ft_concat_params(int argc, char** argv) 94 | main_basic -m ==== 95 | char* arr[5] = {"NO", "YES", "ARG2", "ARG3", NULL}; 96 | printf("%s", ft_concat_params(4, arr)); 97 | ==== check_basic -e ==== 98 | $expected = 'YES 99 | ARG2 100 | ARG3'; 101 | ==== main_short -m ==== 102 | char* arr[3] = {"NO", "YES", NULL}; 103 | printf("%s", ft_concat_params(2, arr)); 104 | ==== check_short -e ==== 105 | $expected = 'YES'; 106 | ==== main_empty -m ==== 107 | char* arr[2] = {"NO", NULL}; 108 | printf("%s", ft_concat_params(1, arr)); 109 | ==== check_empty -e ==== 110 | $expected = ''; 111 | ==== 112 | 113 | 114 | ex04 115 | char** ft_split_whitespaces(char* str) 116 | main_basic -m ==== 117 | char** res; 118 | for (res = ft_split_whitespaces("asdf qwerty zxcv"); *res != 0; res++) 119 | printf("'%s',", *res); 120 | printf("\n"); 121 | for (res = ft_split_whitespaces("s1 s2 \t\n\t\ns3"); *res != 0; res++) 122 | printf("'%s',", *res); 123 | printf("\n"); 124 | ==== check_basic -e ==== 125 | $expected = "'asdf','qwerty','zxcv', 126 | 's1','s2','s3', 127 | "; 128 | ==== main_junk -m ==== 129 | char** res; 130 | for (res = ft_split_whitespaces(" \t\nlol hi \t\n"); *res != 0; res++) 131 | printf("'%s',", *res); 132 | ==== check_junk -e ==== 133 | $expected = "'lol','hi',"; 134 | ==== main_less -m ==== 135 | char** res; 136 | for (res = ft_split_whitespaces(" \t\n"); *res != 0; res++) 137 | printf("'%s',", *res); 138 | printf("\n"); 139 | for (res = ft_split_whitespaces(""); *res != 0; res++) 140 | printf("'%s',", *res); 141 | printf("\n"); 142 | ==== check_less -e ==== 143 | $expected = "\n\n"; 144 | ==== 145 | 146 | 147 | ex05 148 | void ft_print_words_tables(char **tab) 149 | main_basic -m ==== 150 | char* table[] = {"asdf","qwerty","zxcv", NULL}; 151 | ft_print_words_tables(table); 152 | ==== check_basic -e ==== 153 | $expected = 'asdf 154 | qwerty 155 | zxcv 156 | '; 157 | ==== main_empty -m ==== 158 | char* table[] = {NULL}; 159 | ft_print_words_tables(table); 160 | ==== check_empty -e ==== 161 | $expected = ''; 162 | ==== 163 | 164 | 165 | ex06 166 | char* ft_convert_base(char* nbr, char* base_from, char* base_to) 167 | main -m ==== 168 | TEST_STR(ft_convert_base("15", "0123456789", "01"), "1111"); 169 | TEST_STR(ft_convert_base("10000", "01", "0123456789"), "16"); 170 | TEST_STR(ft_convert_base("-31", "0123456789", "0123456789abcdef"), "-1f"); 171 | TEST_STR(ft_convert_base("+1f", "0123456789abcdef", "0123456789"), "31"); 172 | TEST_STR(ft_convert_base("0", "0123456789", "0123456789abcdef"), "0"); 173 | TEST_STR(ft_convert_base("0.1234567", "0123456789", "0123456789"), "0"); 174 | TEST_STR(ft_convert_base("2147483647", "0123456789", "0123456789abcdef"), "7fffffff"); 175 | TEST_STR(ft_convert_base("-80000000", "0123456789abcdef", "0123456789"), "-2147483648"); 176 | ==== check -l=8 ==== 177 | ==== 178 | 179 | 180 | ex07 181 | char** ft_split(char *str, char *charset) 182 | main_basic -m ==== 183 | char** res; 184 | for (res = ft_split("asdf qwerty zxcv", " "); *res != 0; res++) 185 | printf("'%s',", *res); 186 | printf("\n"); 187 | for (res = ft_split("s1_s2___++++___s3", "_+"); *res != 0; res++) 188 | printf("'%s',", *res); 189 | printf("\n"); 190 | ==== check_basic -e ==== 191 | $expected = "'asdf','qwerty','zxcv', 192 | 's1','s2','s3', 193 | "; 194 | ==== main_junk -m ==== 195 | char** res; 196 | for (res = ft_split("++++lol_+_+hi____++++___", "_+"); *res != 0; res++) 197 | printf("'%s',", *res); 198 | ==== check_junk -e ==== 199 | $expected = "'lol','hi',"; 200 | ==== main_less -m ==== 201 | char** res; 202 | for (res = ft_split("NOPENOPENOPE", "NOPE"); *res != 0; res++) 203 | printf("'%s',", *res); 204 | printf("\n"); 205 | for (res = ft_split("", "NOPE"); *res != 0; res++) 206 | printf("'%s',", *res); 207 | printf("\n"); 208 | ==== check_less -e ==== 209 | $expected = "\n\n"; 210 | ==== 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | -------------------------------------------------------------------------------- /config_d08.pl: -------------------------------------------------------------------------------- 1 | 2 | ex00 3 | char** ft_split_whitespaces(char* str) 4 | main_basic -m ==== 5 | char** res; 6 | for (res = ft_split_whitespaces("asdf qwerty zxcv"); *res != 0; res++) 7 | printf("'%s',", *res); 8 | printf("\n"); 9 | for (res = ft_split_whitespaces("s1 s2 \t\n\t\ns3"); *res != 0; res++) 10 | printf("'%s',", *res); 11 | printf("\n"); 12 | ==== check_basic -e ==== 13 | $expected = "'asdf','qwerty','zxcv', 14 | 's1','s2','s3', 15 | "; 16 | ==== main_junk -m ==== 17 | char** res; 18 | for (res = ft_split_whitespaces(" \t\nlol hi \t\n"); *res != 0; res++) 19 | printf("'%s',", *res); 20 | ==== check_junk -e ==== 21 | $expected = "'lol','hi',"; 22 | ==== main_less -m ==== 23 | char** res; 24 | for (res = ft_split_whitespaces(" \t\n"); *res != 0; res++) 25 | printf("'%s',", *res); 26 | printf("\n"); 27 | for (res = ft_split_whitespaces(""); *res != 0; res++) 28 | printf("'%s',", *res); 29 | printf("\n"); 30 | ==== check_less -e ==== 31 | $expected = "\n\n"; 32 | ==== 33 | 34 | 35 | ex01 -N -f=ft.h 36 | ft 37 | source_mock_stuff ==== 38 | #include 39 | 40 | // this is already in stupidity.c 41 | /*void ft_putchar(char c) 42 | { 43 | c++; 44 | printf("ft_putchar\n"); 45 | }*/ 46 | void ft_putstr(char* str) 47 | { 48 | str++; 49 | printf("ft_putstr\n"); 50 | } 51 | int ft_strcmp(char* s1, char* s2) 52 | { 53 | s1++; 54 | s2++; 55 | printf("ft_strcmp\n"); 56 | return 0; 57 | } 58 | 59 | int ft_strlen(char* str) 60 | { 61 | str++; 62 | printf("ft_strlen\n"); 63 | return 0; 64 | } 65 | 66 | void ft_swap(int* a, int* b) 67 | { 68 | a++; 69 | b++; 70 | printf("ft_swap\n"); 71 | } 72 | 73 | 74 | 75 | ==== main -f=mock_stuff.c ==== 76 | #include "ft.h" 77 | #include 78 | int main() 79 | { 80 | ft_putchar('\n'); 81 | ft_putstr(NULL); 82 | ft_strcmp(NULL,NULL); 83 | ft_strlen(NULL); 84 | ft_swap(NULL,NULL); 85 | } 86 | ==== check -e ==== 87 | $expected = ' 88 | ft_putstr 89 | ft_strcmp 90 | ft_strlen 91 | ft_swap 92 | '; 93 | ==== 94 | 95 | 96 | ex02 -N -f=ft_boolean.h 97 | ft_boolean 98 | main ==== 99 | #include "ft_boolean.h" 100 | void ft_putstr(char *str) 101 | { 102 | while (*str) 103 | write(1, str++, 1); 104 | } 105 | 106 | t_bool ft_is_even(int nbr) 107 | { 108 | return ((EVEN(nbr)) ? TRUE : FALSE); 109 | } 110 | 111 | int main(int argc, char **argv) 112 | { 113 | (void)argv; 114 | if (ft_is_even(argc - 1) == TRUE) 115 | ft_putstr(EVEN_MSG); 116 | else 117 | ft_putstr(ODD_MSG); 118 | return (SUCCESS); 119 | } 120 | ==== check -t ==== 121 | %tests = ( 122 | "$program" => "I have an even number of arguments.\n", 123 | "$program asdf" => "I have an odd number of arguments.\n", 124 | "$program asdf qwerty" => "I have an even number of arguments.\n", 125 | ); 126 | ==== 127 | 128 | 129 | ex03 -N -f=ft_abs.h 130 | abs 131 | main ==== 132 | #include "ft_abs.h" 133 | #include 134 | int main() 135 | { 136 | TEST(ABS(15), 15); 137 | TEST(ABS(0), 0); 138 | TEST(ABS(-25), 25); 139 | TEST(ABS(1234567), 1234567); 140 | TEST(ABS(-1234567), 1234567); 141 | TEST(ABS(2147483647), 2147483647); 142 | // im not sure about this one actually... use your head 143 | TEST(ABS((int)-2147483648), 0); 144 | } 145 | ==== check -l=7 ==== 146 | ==== 147 | 148 | 149 | ex04 -N -f=ft_point.h 150 | point 151 | main ==== 152 | #include "ft_point.h" 153 | #include 154 | void set_point(t_point *point) 155 | { 156 | point->x = 42; 157 | point->y = 21; 158 | } 159 | int main(void) 160 | { 161 | t_point point; 162 | set_point(&point); 163 | printf("%d,%d", point.x, point.y); 164 | } 165 | ==== check -e ==== 166 | $expected = ((('42,21'))); 167 | ==== 168 | 169 | 170 | ex05 -N -f1=ft_param_to_tab.c -f2=ft_stock_par.h 171 | param 172 | source_split_whitespaces ==== 173 | #include 174 | int my_isspace(char c) 175 | { 176 | return (c == ' ' || c == '\n' || c == '\t'); 177 | } 178 | 179 | int my_spaced_strlen(char *str) 180 | { 181 | int len; 182 | 183 | len = 0; 184 | while (*str != 0 && !my_isspace(*str)) 185 | { 186 | len++; 187 | str++; 188 | } 189 | return (len); 190 | } 191 | 192 | char *my_strndup(char *source, int n) 193 | { 194 | char *res; 195 | int i; 196 | 197 | res = malloc(n + 1); 198 | if (res == 0) 199 | return (0); 200 | i = 0; 201 | while (i < n) 202 | res[i++] = *source++; 203 | res[i] = 0; 204 | return (res); 205 | } 206 | 207 | char **ft_split_whitespaces(char *str) 208 | { 209 | char **res; 210 | int i; 211 | char *iter; 212 | char **dest; 213 | 214 | i = 0; 215 | iter = str; 216 | while (*iter != 0) 217 | { 218 | i += 0 != my_spaced_strlen(iter); 219 | iter += my_spaced_strlen(iter); 220 | iter += *iter != 0; 221 | } 222 | res = malloc((i + 1) * sizeof(char*)); 223 | iter = str; 224 | dest = res; 225 | while (*iter != 0) 226 | { 227 | if (my_spaced_strlen(iter) > 0) 228 | *dest++ = my_strndup(iter, my_spaced_strlen(iter)); 229 | iter += my_spaced_strlen(iter); 230 | iter += *iter != 0; 231 | } 232 | *dest = 0; 233 | return (res); 234 | } 235 | ==== main -f1=ft_param_to_tab.c -f2=split_whitespaces.c ==== 236 | #include "ft_stock_par.h" 237 | struct s_stock_par *ft_param_to_tab(int ac, char **av); 238 | int main(int argc, char** argv) 239 | { 240 | if (argc == 0) 241 | return 1; 242 | struct s_stock_par* params = ft_param_to_tab(argc, argv); 243 | while (params->str != 0) 244 | { 245 | printf("%d,%s,%s, is_duped? %d\n", params->size_param, params->str, params->copy, params->str != params->copy); 246 | char** words = params->tab; 247 | while (*words != 0) 248 | { 249 | printf("'%s',", *words); 250 | words++; 251 | } 252 | printf("\n"); 253 | params++; 254 | } 255 | } 256 | ==== check -t ==== 257 | %tests = ( 258 | "$program" => length($program) . ",$program,$program, is_duped? 1\n'$program',\n", 259 | "$program 'asdf qwer'" => length($program) . ",$program,$program, is_duped? 1\n'$program', 260 | 9,asdf qwer,asdf qwer, is_duped? 1\n'asdf','qwer',\n", 261 | "$program ' asdf qwer ' ''" => length($program) . ",$program,$program, is_duped? 1\n'$program', 262 | 11, asdf qwer , asdf qwer , is_duped? 1\n'asdf','qwer', 263 | 0,,, is_duped? 1\n\n", 264 | ); 265 | ==== 266 | 267 | 268 | ex06 -N -f1=ft_show_tab.c -f2=ft_stock_par.h 269 | show_tab 270 | main_basic -f=ft_show_tab.c ==== 271 | #include "ft_stock_par.h" 272 | void ft_show_tab(struct s_stock_par* par); 273 | int main() 274 | { 275 | t_stock_par stuff[2]; 276 | char* words[] = {"lol","hijack", NULL}; 277 | stuff[0].size_param = 10; 278 | stuff[0].str = "hello world!"; 279 | stuff[0].copy = "hello world!"; 280 | stuff[0].tab = words; 281 | 282 | stuff[1].str = NULL; 283 | 284 | ft_show_tab(stuff); 285 | } 286 | ==== check_basic -e ==== 287 | $expected = 'hello world! 288 | 10 289 | lol 290 | hijack 291 | '; 292 | ==== main_empty -f=ft_show_tab.c ==== 293 | #include "ft_stock_par.h" 294 | void ft_show_tab(struct s_stock_par* par); 295 | int main() 296 | { 297 | t_stock_par stuff[2]; 298 | char* words[] = {NULL}; 299 | stuff[0].size_param = 0; 300 | stuff[0].str = ""; 301 | stuff[0].copy = ""; 302 | stuff[0].tab = words; 303 | 304 | stuff[1].str = NULL; 305 | 306 | ft_show_tab(stuff); 307 | } 308 | ==== check_basic -e ==== 309 | $expected = ' 310 | 0 311 | '; 312 | ==== 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | -------------------------------------------------------------------------------- /config_d04.pl: -------------------------------------------------------------------------------- 1 | ex00 2 | int ft_iterative_factorial(int nb) 3 | main -m ==== 4 | int res, exp; 5 | 6 | res = ft_iterative_factorial(4), exp = 24; 7 | printf("4! (%d vs %d) -> %d\n", res, exp, res == exp); 8 | res = ft_iterative_factorial(0), exp = 1; 9 | printf("0! (%d vs %d) -> %d\n", res, exp, res == exp); 10 | res = ft_iterative_factorial(1), exp = 1; 11 | printf("1! (%d vs %d) -> %d\n", res, exp, res == exp); 12 | res = ft_iterative_factorial(10), exp = 3628800; 13 | printf("10! (%d vs %d) -> %d\n", res, exp, res == exp); 14 | res = ft_iterative_factorial(12), exp = 479001600; 15 | printf("12! (%d vs %d) -> %d\n", res, exp, res == exp); 16 | res = ft_iterative_factorial(13), exp = 0; 17 | printf("13! (%d vs %d) -> %d\n", res, exp, res == exp); 18 | res = ft_iterative_factorial(-2), exp = 0; 19 | printf("-2! (%d vs %d) -> %d\n", res, exp, res == exp); 20 | res = ft_iterative_factorial(1000), exp = 0; 21 | printf("1000! (%d vs %d) -> %d\n", res, exp, res == exp); 22 | ==== check -l=8 ==== 23 | ==== 24 | 25 | 26 | ex01 27 | int ft_recursive_factorial(int nb) 28 | main -m ==== 29 | int res, exp; 30 | 31 | res = ft_recursive_factorial(4), exp = 24; 32 | printf("4! (%d vs %d) -> %d\n", res, exp, res == exp); 33 | res = ft_recursive_factorial(0), exp = 1; 34 | printf("0! (%d vs %d) -> %d\n", res, exp, res == exp); 35 | res = ft_recursive_factorial(1), exp = 1; 36 | printf("1! (%d vs %d) -> %d\n", res, exp, res == exp); 37 | res = ft_recursive_factorial(10), exp = 3628800; 38 | printf("10! (%d vs %d) -> %d\n", res, exp, res == exp); 39 | res = ft_recursive_factorial(12), exp = 479001600; 40 | printf("12! (%d vs %d) -> %d\n", res, exp, res == exp); 41 | res = ft_recursive_factorial(13), exp = 0; 42 | printf("13! (%d vs %d) -> %d\n", res, exp, res == exp); 43 | res = ft_recursive_factorial(-2), exp = 0; 44 | printf("-2! (%d vs %d) -> %d\n", res, exp, res == exp); 45 | res = ft_recursive_factorial(1000), exp = 0; 46 | printf("1000! (%d vs %d) -> %d\n", res, exp, res == exp); 47 | ==== check -l=8 ==== 48 | ==== 49 | 50 | 51 | ex02 52 | int ft_iterative_power(int nb, int power) 53 | main -m ==== 54 | int res, exp; 55 | 56 | res = ft_iterative_power(2, 2), exp = 4; 57 | printf("2^2 (%d vs %d) -> %d\n", res, exp, res == exp); 58 | res = ft_iterative_power(2, 1), exp = 2; 59 | printf("2^1 (%d vs %d) -> %d\n", res, exp, res == exp); 60 | res = ft_iterative_power(2, 0), exp = 1; 61 | printf("2^0 (%d vs %d) -> %d\n", res, exp, res == exp); 62 | res = ft_iterative_power(2, -1), exp = 0; 63 | printf("2^-1 (%d vs %d) -> %d\n", res, exp, res == exp); 64 | res = ft_iterative_power(2, 4), exp = 16; 65 | printf("2^4 (%d vs %d) -> %d\n", res, exp, res == exp); 66 | res = ft_iterative_power(5, 3), exp = 125; 67 | printf("5^3 (%d vs %d) -> %d\n", res, exp, res == exp); 68 | res = ft_iterative_power(3, 5), exp = 243; 69 | printf("3^5 (%d vs %d) -> %d\n", res, exp, res == exp); 70 | res = ft_iterative_power(2, 1000000), exp = -1; 71 | printf("2^1000000 (%d vs %d) -> %d\n", res, exp, 1); 72 | ==== check -l=8 ==== 73 | ==== 74 | 75 | 76 | ex03 77 | int ft_recursive_power(int nb, int power) 78 | main -m ==== 79 | int res, exp; 80 | 81 | res = ft_recursive_power(2, 2), exp = 4; 82 | printf("2^2 (%d vs %d) -> %d\n", res, exp, res == exp); 83 | res = ft_recursive_power(2, 1), exp = 2; 84 | printf("2^1 (%d vs %d) -> %d\n", res, exp, res == exp); 85 | res = ft_recursive_power(2, 0), exp = 1; 86 | printf("2^0 (%d vs %d) -> %d\n", res, exp, res == exp); 87 | res = ft_recursive_power(2, -1), exp = 0; 88 | printf("2^-1 (%d vs %d) -> %d\n", res, exp, res == exp); 89 | res = ft_recursive_power(2, 4), exp = 16; 90 | printf("2^4 (%d vs %d) -> %d\n", res, exp, res == exp); 91 | res = ft_recursive_power(5, 3), exp = 125; 92 | printf("5^3 (%d vs %d) -> %d\n", res, exp, res == exp); 93 | res = ft_recursive_power(3, 5), exp = 243; 94 | printf("3^5 (%d vs %d) -> %d\n", res, exp, res == exp); 95 | res = ft_recursive_power(2, 1000000), exp = -1; 96 | printf("2^1000000 (%d vs %d) -> %d\n", res, exp, 1); 97 | ==== check -l=8 ==== 98 | ==== 99 | 100 | 101 | ex04 102 | int ft_fibonacci(int index) 103 | main -m ==== 104 | printf("%d,%d,%d,%d, %d,%d,%d,%d, %d,%d,%d,%d", 105 | ft_fibonacci(0), 106 | ft_fibonacci(1), 107 | ft_fibonacci(2), 108 | ft_fibonacci(3), 109 | 110 | ft_fibonacci(4), 111 | ft_fibonacci(5), 112 | ft_fibonacci(6), 113 | ft_fibonacci(7), 114 | 115 | ft_fibonacci(8), 116 | ft_fibonacci(9), 117 | ft_fibonacci(10), 118 | ft_fibonacci(11)); 119 | ==== check -e ==== 120 | $expected = '0,1,1,2, 3,5,8,13, 21,34,55,89'; 121 | ==== 122 | 123 | 124 | ex05 125 | int ft_sqrt(int nb) 126 | main -m ==== 127 | int res, exp; 128 | 129 | res = ft_sqrt(4), exp = 2; 130 | printf("root 4 (%d vs %d) -> %d\n", res, exp, res == exp); 131 | res = ft_sqrt(1), exp = 1; 132 | printf("root 1 (%d vs %d) -> %d\n", res, exp, res == exp); 133 | res = ft_sqrt(0), exp = 0; 134 | printf("root 0 (%d vs %d) -> %d\n", res, exp, res == exp); 135 | res = ft_sqrt(3), exp = 0; 136 | printf("root 3 (%d vs %d) -> %d\n", res, exp, res == exp); 137 | res = ft_sqrt(9), exp = 3; 138 | printf("root 9 (%d vs %d) -> %d\n", res, exp, res == exp); 139 | res = ft_sqrt(16), exp = 4; 140 | printf("root 16 (%d vs %d) -> %d\n", res, exp, res == exp); 141 | res = ft_sqrt(-5), exp = 0; 142 | printf("root -5 (%d vs %d) -> %d\n", res, exp, res == exp); 143 | res = ft_sqrt(144), exp = 12; 144 | printf("root 144 (%d vs %d) -> %d\n", res, exp, res == exp); 145 | ==== check -l=8 ==== 146 | ==== 147 | 148 | 149 | 150 | 151 | 152 | ex06 153 | int ft_is_prime(int nb) 154 | main -m ==== 155 | int res, exp; 156 | 157 | res = ft_is_prime(4), exp = 0; 158 | printf("prime 4? (%d vs %d) -> %d\n", res, exp, res == exp); 159 | res = ft_is_prime(2), exp = 1; 160 | printf("prime 2? (%d vs %d) -> %d\n", res, exp, res == exp); 161 | res = ft_is_prime(1), exp = 0; 162 | printf("prime 1? (%d vs %d) -> %d\n", res, exp, res == exp); 163 | res = ft_is_prime(0), exp = 0; 164 | printf("prime 0? (%d vs %d) -> %d\n", res, exp, res == exp); 165 | res = ft_is_prime(7), exp = 1; 166 | printf("prime 7? (%d vs %d) -> %d\n", res, exp, res == exp); 167 | res = ft_is_prime(23), exp = 1; 168 | printf("prime 23? (%d vs %d) -> %d\n", res, exp, res == exp); 169 | res = ft_is_prime(-5), exp = 0; 170 | printf("prime -5? (%d vs %d) -> %d\n", res, exp, res == exp); 171 | res = ft_is_prime(61), exp = 1; 172 | printf("prime 61? (%d vs %d) -> %d\n", res, exp, res == exp); 173 | ==== check -l=8 ==== 174 | ==== 175 | 176 | 177 | ex07 178 | int ft_find_next_prime(int nb) 179 | main -p -m ==== 180 | $code = "int res, exp;\n"; 181 | 182 | my %tests = ( 183 | -2 => 2, 184 | -1 => 2, 185 | 0 => 2, 186 | 1 => 2, 187 | 188 | 2 => 2, 189 | 3 => 3, 190 | 4 => 5, 191 | 5 => 5, 192 | 193 | 6 => 7, 194 | 8 => 11, 195 | 14 => 17, 196 | 20 => 23, 197 | ); 198 | 199 | foreach my $arg (sort keys %tests) { 200 | $code .= "res = ft_find_next_prime($arg), exp = $tests{$arg};\n"; 201 | $code .= "printf(\"next($arg) ($tests{$arg} vs %d) -> %d\\n\", res, res == exp);\n"; 202 | } 203 | ==== check -l=12 ==== 204 | ==== 205 | 206 | 207 | 208 | ex08 209 | int ft_eight_queens_puzzle(void) 210 | main -m ==== 211 | printf("%d", ft_eight_queens_puzzle()); 212 | ==== check -e ==== 213 | $expected = '92'; 214 | ==== 215 | 216 | 217 | ex09 218 | ft_eight_queens_puzzle_2 219 | main -m ==== 220 | ft_eight_queens_puzzle_2(); 221 | ==== check -e ==== 222 | $expected = "15863724 223 | 16837425 224 | 17468253 225 | 17582463 226 | 24683175 227 | 25713864 228 | 25741863 229 | 26174835 230 | 26831475 231 | 27368514 232 | 27581463 233 | 28613574 234 | 31758246 235 | 35281746 236 | 35286471 237 | 35714286 238 | 35841726 239 | 36258174 240 | 36271485 241 | 36275184 242 | 36418572 243 | 36428571 244 | 36814752 245 | 36815724 246 | 36824175 247 | 37285146 248 | 37286415 249 | 38471625 250 | 41582736 251 | 41586372 252 | 42586137 253 | 42736815 254 | 42736851 255 | 42751863 256 | 42857136 257 | 42861357 258 | 46152837 259 | 46827135 260 | 46831752 261 | 47185263 262 | 47382516 263 | 47526138 264 | 47531682 265 | 48136275 266 | 48157263 267 | 48531726 268 | 51468273 269 | 51842736 270 | 51863724 271 | 52468317 272 | 52473861 273 | 52617483 274 | 52814736 275 | 53168247 276 | 53172864 277 | 53847162 278 | 57138642 279 | 57142863 280 | 57248136 281 | 57263148 282 | 57263184 283 | 57413862 284 | 58413627 285 | 58417263 286 | 61528374 287 | 62713584 288 | 62714853 289 | 63175824 290 | 63184275 291 | 63185247 292 | 63571428 293 | 63581427 294 | 63724815 295 | 63728514 296 | 63741825 297 | 64158273 298 | 64285713 299 | 64713528 300 | 64718253 301 | 68241753 302 | 71386425 303 | 72418536 304 | 72631485 305 | 73168524 306 | 73825164 307 | 74258136 308 | 74286135 309 | 75316824 310 | 82417536 311 | 82531746 312 | 83162574 313 | 84136275"; 314 | $output = join "\n", sort split "\n", $output; 315 | ==== 316 | -------------------------------------------------------------------------------- /config_dmatch.pl: -------------------------------------------------------------------------------- 1 | ex00 2 | int match(char* s1, char* s2) 3 | main -m ==== 4 | 5 | TEST(match("asdf", "asdf"), 1); 6 | TEST(match("", ""), 1); 7 | TEST(match("a", "a"), 1); 8 | TEST(match("aa", "aa"), 1); 9 | TEST(match("aaa", "aaa"), 1); 10 | 11 | TEST(match("", "asdf"), 0); 12 | TEST(match("asdf", ""), 0); 13 | TEST(match("asdf", "a"), 0); 14 | TEST(match("a", "asdf"), 0); 15 | TEST(match("asdf", "asde"), 0); 16 | 17 | TEST(match("asde", "asdf"), 0); 18 | TEST(match("asdf", "asdff"), 0); 19 | TEST(match("asdff", "asdf"), 0); 20 | TEST(match("aasdf", "asdf"), 0); 21 | TEST(match("asdf", "aasdf"), 0); 22 | 23 | TEST(match("", "*"), 1); 24 | TEST(match("a", "*"), 1); 25 | TEST(match("aa", "*"), 1); 26 | TEST(match("asdf", "*"), 1); 27 | TEST(match("asdfasdf", "*"), 1); 28 | 29 | TEST(match("asdf", "a*"), 1); 30 | TEST(match("asdfasdf", "a*"), 1); 31 | TEST(match("a", "a*"), 1); 32 | TEST(match("aa", "a*"), 1); 33 | TEST(match("aasdf", "a*"), 1); 34 | 35 | TEST(match("", "a*"), 0); 36 | TEST(match("f", "a*"), 0); 37 | TEST(match("fdsa", "a*"), 0); 38 | TEST(match("faaaa", "a*"), 0); 39 | TEST(match("faaasdf", "a*"), 0); 40 | 41 | TEST(match("fdsa", "*a"), 1); 42 | TEST(match("a", "*a"), 1); 43 | TEST(match("aa", "*a"), 1); 44 | TEST(match("fdsaaaaaaafdsa", "*a"), 1); 45 | TEST(match("dfsaaaaaaaaaaaa", "*a"), 1); 46 | 47 | TEST(match("", "*a"), 0); 48 | TEST(match("f", "*a"), 0); 49 | TEST(match("asdf", "*a"), 0); 50 | TEST(match("aaaaaf", "*a"), 0); 51 | TEST(match("fdsaaaaf", "*a"), 0); 52 | 53 | TEST(match("abc", "abc*"), 1); 54 | TEST(match("abcdef", "abc*"), 1); 55 | TEST(match("abccccccc", "abc*"), 1); 56 | TEST(match("abcabc", "abc*"), 1); 57 | TEST(match("abcdabcdabc", "abc*"), 1); 58 | 59 | TEST(match("", "abc*"), 0); 60 | TEST(match("f", "abc*"), 0); 61 | TEST(match("abbc", "abc*"), 0); 62 | TEST(match("ab", "abc*"), 0); 63 | TEST(match("abb", "abc*"), 0); 64 | // 50 65 | 66 | TEST(match("abc", "*abc"), 1); 67 | TEST(match("defabc", "*abc"), 1); 68 | TEST(match("aaaaaaabc", "*abc"), 1); 69 | TEST(match("abcabc", "*abc"), 1); 70 | TEST(match("abcdabcdabc", "*abc"), 1); 71 | 72 | TEST(match("", "*abc"), 0); 73 | TEST(match("f", "*abc"), 0); 74 | TEST(match("abbc", "*abc"), 0); 75 | TEST(match("ab", "*abc"), 0); 76 | TEST(match("abb", "*abc"), 0); 77 | 78 | TEST(match("a", "*a*"), 1); 79 | TEST(match("aaa", "*a*"), 1); 80 | TEST(match("aaaaaaabc", "*a*"), 1); 81 | TEST(match("bcabc", "*a*"), 1); 82 | TEST(match("sdfa", "*a*"), 1); 83 | 84 | TEST(match("", "*a*"), 0); 85 | TEST(match("f", "*a*"), 0); 86 | TEST(match("bcd", "*a*"), 0); 87 | TEST(match("bb", "*a*"), 0); 88 | TEST(match("ASDF", "*a*"), 0); 89 | 90 | TEST(match("ab", "*ab*"), 1); 91 | TEST(match("abcd", "*ab*"), 1); 92 | TEST(match("efabcd", "*ab*"), 1); 93 | TEST(match("ababab", "*ab*"), 1); 94 | TEST(match("bab", "*ab*"), 1); 95 | 96 | TEST(match("asdf", "*ab*"), 0); 97 | TEST(match("ba", "*ab*"), 0); 98 | TEST(match("", "*ab*"), 0); 99 | TEST(match("f", "*ab*"), 0); 100 | TEST(match("bbbbbbaaaaa", "*ab*"), 0); 101 | 102 | TEST(match("", "**"), 1); 103 | TEST(match("a", "**"), 1); 104 | TEST(match("ab", "**"), 1); 105 | TEST(match("abc", "**"), 1); 106 | TEST(match("abcd", "**"), 1); 107 | 108 | TEST(match("a", "a**"), 1); 109 | TEST(match("ab", "a**"), 1); 110 | TEST(match("abc", "a**"), 1); 111 | TEST(match("aaaaaa", "a**"), 1); 112 | TEST(match("asdfaaaaa", "a**"), 1); 113 | 114 | TEST(match("", "a**"), 0); 115 | TEST(match("f", "a**"), 0); 116 | TEST(match("fa", "a**"), 0); 117 | TEST(match("faaaaaaa", "a**"), 0); 118 | TEST(match("fafdsafdsa", "a**"), 0); 119 | 120 | TEST(match("asdf", "*a**"), 1); 121 | TEST(match("sdfa", "*a**"), 1); 122 | TEST(match("faffff", "*a**"), 1); 123 | TEST(match("fdsaasdf", "*a**"), 1); 124 | TEST(match("aaaaaaa", "*a**"), 1); 125 | // 100 126 | 127 | TEST(match("", "*a**"), 0); 128 | TEST(match("qwerqwer", "*a**"), 0); 129 | TEST(match("bbbbbb", "*a**"), 0); 130 | TEST(match("c", "*a**"), 0); 131 | TEST(match("AAAAAAAAAA", "*a**"), 0); 132 | 133 | TEST(match("main.c", "*.c"), 1); 134 | TEST(match("main.c.c", "*.c"), 1); 135 | TEST(match("main.h", "*.c"), 0); 136 | TEST(match("main.cc", "*.c"), 0); 137 | TEST(match("main.c", "*.*"), 1); 138 | 139 | TEST(match("test.main.c", "test.*.c"), 1); 140 | TEST(match("test..c", "test.*.c"), 1); 141 | TEST(match("test.main.h", "test.*.c"), 0); 142 | TEST(match("main.c", "test.*.c"), 0); 143 | TEST(match("test.c", "test.*.c"), 0); 144 | 145 | TEST(match("abcde", "a*c*e"), 1); 146 | TEST(match("abcde", "*b*d*"), 1); 147 | TEST(match("abcde", "a*e*c"), 0); 148 | TEST(match("abcde", "*d*c*"), 0); 149 | TEST(match("abcde", "a*d*d"), 0); 150 | 151 | TEST(match("*a", "*"), 1); 152 | TEST(match("**", "*"), 1); 153 | TEST(match("*ab", "*b"), 1); 154 | TEST(match("a*a", "a*"), 1); 155 | TEST(match("**a", "*a"), 1); 156 | 157 | 158 | ==== check -l=125 ==== 159 | ==== 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | ex01 168 | int nmatch(char* s1, char* s2) 169 | main -m ==== 170 | 171 | TEST(nmatch("asdf", "asdf"), 1); 172 | TEST(nmatch("", ""), 1); 173 | TEST(nmatch("a", "a"), 1); 174 | TEST(nmatch("aa", "aa"), 1); 175 | TEST(nmatch("aaa", "aaa"), 1); 176 | 177 | TEST(nmatch("", "asdf"), 0); 178 | TEST(nmatch("asdf", ""), 0); 179 | TEST(nmatch("asdf", "a"), 0); 180 | TEST(nmatch("a", "asdf"), 0); 181 | TEST(nmatch("asdf", "asde"), 0); 182 | 183 | TEST(nmatch("asde", "asdf"), 0); 184 | TEST(nmatch("asdf", "asdff"), 0); 185 | TEST(nmatch("asdff", "asdf"), 0); 186 | TEST(nmatch("aasdf", "asdf"), 0); 187 | TEST(nmatch("asdf", "aasdf"), 0); 188 | 189 | TEST(nmatch("", "*"), 1); 190 | TEST(nmatch("a", "*"), 1); 191 | TEST(nmatch("aa", "*"), 1); 192 | TEST(nmatch("asdf", "*"), 1); 193 | TEST(nmatch("asdfasdf", "*"), 1); 194 | 195 | TEST(nmatch("asdf", "a*"), 1); 196 | TEST(nmatch("asdfasdf", "a*"), 1); 197 | TEST(nmatch("a", "a*"), 1); 198 | TEST(nmatch("aa", "a*"), 1); 199 | TEST(nmatch("aasdf", "a*"), 1); 200 | 201 | TEST(nmatch("", "a*"), 0); 202 | TEST(nmatch("f", "a*"), 0); 203 | TEST(nmatch("fdsa", "a*"), 0); 204 | TEST(nmatch("faaaa", "a*"), 0); 205 | TEST(nmatch("faaasdf", "a*"), 0); 206 | 207 | TEST(nmatch("fdsa", "*a"), 1); 208 | TEST(nmatch("a", "*a"), 1); 209 | TEST(nmatch("aa", "*a"), 1); 210 | TEST(nmatch("fdsaaaaaaafdsa", "*a"), 1); 211 | TEST(nmatch("dfsaaaaaaaaaaaa", "*a"), 1); 212 | 213 | TEST(nmatch("", "*a"), 0); 214 | TEST(nmatch("f", "*a"), 0); 215 | TEST(nmatch("asdf", "*a"), 0); 216 | TEST(nmatch("aaaaaf", "*a"), 0); 217 | TEST(nmatch("fdsaaaaf", "*a"), 0); 218 | 219 | TEST(nmatch("abc", "abc*"), 1); 220 | TEST(nmatch("abcdef", "abc*"), 1); 221 | TEST(nmatch("abccccccc", "abc*"), 1); 222 | TEST(nmatch("abcabc", "abc*"), 1); 223 | TEST(nmatch("abcdabcdabc", "abc*"), 1); 224 | 225 | TEST(nmatch("", "abc*"), 0); 226 | TEST(nmatch("f", "abc*"), 0); 227 | TEST(nmatch("abbc", "abc*"), 0); 228 | TEST(nmatch("ab", "abc*"), 0); 229 | TEST(nmatch("abb", "abc*"), 0); 230 | // 50 231 | 232 | TEST(nmatch("abc", "*abc"), 1); 233 | TEST(nmatch("defabc", "*abc"), 1); 234 | TEST(nmatch("aaaaaaabc", "*abc"), 1); 235 | TEST(nmatch("abcabc", "*abc"), 1); 236 | TEST(nmatch("abcdabcdabc", "*abc"), 1); 237 | 238 | TEST(nmatch("", "*abc"), 0); 239 | TEST(nmatch("f", "*abc"), 0); 240 | TEST(nmatch("abbc", "*abc"), 0); 241 | TEST(nmatch("ab", "*abc"), 0); 242 | TEST(nmatch("abb", "*abc"), 0); 243 | 244 | TEST(nmatch("a", "*a*"), 1); 245 | TEST(nmatch("aaa", "*a*"), 3); 246 | TEST(nmatch("aaabc", "*a*"), 3); 247 | TEST(nmatch("bcabc", "*a*"), 1); 248 | TEST(nmatch("sdfa", "*a*"), 1); 249 | 250 | TEST(nmatch("", "*a*"), 0); 251 | TEST(nmatch("f", "*a*"), 0); 252 | TEST(nmatch("bcd", "*a*"), 0); 253 | TEST(nmatch("bb", "*a*"), 0); 254 | TEST(nmatch("ASDF", "*a*"), 0); 255 | 256 | TEST(nmatch("ab", "*ab*"), 1); 257 | TEST(nmatch("abcd", "*ab*"), 1); 258 | TEST(nmatch("efabcd", "*ab*"), 1); 259 | TEST(nmatch("ababab", "*ab*"), 3); 260 | TEST(nmatch("bab", "*ab*"), 1); 261 | 262 | TEST(nmatch("asdf", "*ab*"), 0); 263 | TEST(nmatch("ba", "*ab*"), 0); 264 | TEST(nmatch("", "*ab*"), 0); 265 | TEST(nmatch("f", "*ab*"), 0); 266 | TEST(nmatch("bbbbbbaaaaa", "*ab*"), 0); 267 | 268 | TEST(nmatch("", "**"), 1); 269 | TEST(nmatch("a", "**"), 2); 270 | TEST(nmatch("ab", "**"), 3); 271 | TEST(nmatch("abc", "**"), 4); 272 | TEST(nmatch("abcd", "**"), 5); 273 | 274 | TEST(nmatch("a", "a**"), 1); 275 | TEST(nmatch("ab", "a**"), 2); 276 | TEST(nmatch("abc", "a**"), 3); 277 | TEST(nmatch("aaaa", "a**"), 4); 278 | TEST(nmatch("asdfa", "a**"), 5); 279 | 280 | TEST(nmatch("", "a**"), 0); 281 | TEST(nmatch("f", "a**"), 0); 282 | TEST(nmatch("fa", "a**"), 0); 283 | TEST(nmatch("faaaaaaa", "a**"), 0); 284 | TEST(nmatch("fafdsafdsa", "a**"), 0); 285 | 286 | TEST(nmatch("asdf", "*a**"), 4); 287 | TEST(nmatch("sdfa", "*a**"), 1); 288 | TEST(nmatch("faffff", "*a**"), 5); 289 | TEST(nmatch("fdsaasdf", "*a**"), 9); 290 | TEST(nmatch("aaaaaaa", "*a**"), 28); 291 | // 100 292 | 293 | TEST(nmatch("", "*a**"), 0); 294 | TEST(nmatch("qwerqwer", "*a**"), 0); 295 | TEST(nmatch("bbbbbb", "*a**"), 0); 296 | TEST(nmatch("c", "*a**"), 0); 297 | TEST(nmatch("AAAAAAAAAA", "*a**"), 0); 298 | 299 | TEST(nmatch("main.c", "*.c"), 1); 300 | TEST(nmatch("main.c.c", "*.c"), 1); 301 | TEST(nmatch("main.h", "*.c"), 0); 302 | TEST(nmatch("main.cc", "*.c"), 0); 303 | TEST(nmatch("main.c", "*.*"), 1); 304 | 305 | TEST(nmatch("test.main.c", "test.*.c"), 1); 306 | TEST(nmatch("test..c", "test.*.c"), 1); 307 | TEST(nmatch("test.main.h", "test.*.c"), 0); 308 | TEST(nmatch("main.c", "test.*.c"), 0); 309 | TEST(nmatch("test.c", "test.*.c"), 0); 310 | 311 | TEST(nmatch("abcde", "a*c*e"), 1); 312 | TEST(nmatch("abcde", "*b*d*"), 1); 313 | TEST(nmatch("abcde", "a*e*c"), 0); 314 | TEST(nmatch("abcde", "*d*c*"), 0); 315 | TEST(nmatch("abcde", "a*d*d"), 0); 316 | 317 | TEST(nmatch("abcbd", "*b*"), 2); 318 | TEST(nmatch("abc", "a**"), 3); 319 | TEST(nmatch("test.asdf.c.bak", "*.*.*"), 3); 320 | TEST(nmatch("test.bak", "*.*.*"), 0); 321 | TEST(nmatch("", "************************"), 1); 322 | 323 | TEST(nmatch("*a", "*"), 1); 324 | TEST(nmatch("**", "*"), 1); 325 | TEST(nmatch("*ab", "*b"), 1); 326 | TEST(nmatch("a*a", "a*"), 1); 327 | TEST(nmatch("**a", "*a"), 1); 328 | 329 | 330 | ==== check -l=130 ==== 331 | ==== 332 | 333 | 334 | 335 | 336 | -------------------------------------------------------------------------------- /config_d05.pl: -------------------------------------------------------------------------------- 1 | ex00 2 | ft_putstr(char *str) 3 | main -m ==== 4 | ft_putstr("asdf"); 5 | ft_putstr(" qwerty\n"); 6 | ft_putstr("zxcv"); 7 | ==== check -e ==== 8 | $expected = "asdf qwerty\nzxcv"; 9 | ==== 10 | 11 | 12 | ex01 13 | ft_putnbr(int nb) 14 | main_basic -m ==== 15 | ft_putnbr(123456); 16 | ==== check_basic -e ==== 17 | $expected = '123456'; 18 | ==== main_negative -m ==== 19 | ft_putnbr(-987654321); 20 | ==== check_negative -e ==== 21 | $expected = '-987654321'; 22 | ==== main_zero -m ==== 23 | ft_putnbr(0); 24 | ==== check_zero -e ==== 25 | $expected = '0'; 26 | ==== main_intmax -m ==== 27 | ft_putnbr(2147483647); 28 | ==== check_intmax -e ==== 29 | $expected = '2147483647'; 30 | ==== main_intnmax -m ==== 31 | ft_putnbr(-2147483648); 32 | ==== check_intnmax -e ==== 33 | $expected = '-2147483648'; 34 | ==== 35 | 36 | 37 | ex02 38 | int ft_atoi(char *str) 39 | main_basic -p -m ==== 40 | $code = 'int res; int exp;'; 41 | my @tests = qw/ 0 15 -25 12345 987654321 -34567 2147483647 -2147483648 /; 42 | foreach (@tests) { 43 | $code .= "res = ft_atoi(\"$_\"), exp = $_;\n"; 44 | $code .= "printf(\"ft_atoi('$_') ($_ vs %d) -> %d\\n\", res, res == exp);\n"; 45 | } 46 | ==== check_basic -l=6 ==== 47 | ==== main_junk -m ==== 48 | TEST(ft_atoi("\t\n\v\f\r +256"), 256); 49 | TEST(ft_atoi("256a99999"), 256); 50 | ==== check_jump -l=2 ==== 51 | ==== 52 | 53 | 54 | ex03 55 | char* ft_strcpy(char* dest, char* src) 56 | main -m ==== 57 | char test1[256] = "asdf"; 58 | printf("%s", ft_strcpy(test1, "qwerty\n")); 59 | printf("%s", ft_strcpy(test1, "")); 60 | printf("%s", ft_strcpy(test1, "hell0\n")); 61 | ==== check -e ==== 62 | $expected = "qwerty\nhell0\n"; 63 | ==== 64 | 65 | 66 | ex04 67 | char* ft_strncpy(char* dest, unsigned int n) 68 | main -m ==== 69 | char test1[256] = "asdf"; 70 | printf("%s\n", ft_strncpy(test1, "uiop", 5)); 71 | printf("%s\n", ft_strncpy(test1, "qwerty", 4)); 72 | printf("%s\n", ft_strncpy(test1, "z", 1)); 73 | ==== check -e ==== 74 | $expected = "uiop\nqwer\nzwer\n"; 75 | ==== 76 | 77 | 78 | ex05 79 | char* ft_strstr(char* str, char* to_find) 80 | main -m ==== 81 | printf("%s\n", ft_strstr("asdf qwerty", "wer")); 82 | printf("%s\n", ft_strstr("asdf qwerty qwerty", "wer")); 83 | printf("%s\n", ft_strstr("asdf qwerty", "qwerty1")); 84 | printf("%s\n", ft_strstr("", "wer")); 85 | printf("%s\n", ft_strstr("asdf qwerty", "zxcv")); 86 | printf("%s\n", ft_strstr("asdf qwerty", "")); 87 | ==== check -e ==== 88 | $expected = "werty\nwerty qwerty\n(null)\n(null)\n(null)\nasdf qwerty\n"; 89 | ==== 90 | 91 | 92 | ex06 93 | int ft_strcmp(char* s1, char* s2) 94 | main -p -m ==== 95 | my %tests = ( 96 | asdf_asdf => 0, 97 | asde_asdf => -1, 98 | asdg_asdf => 1, 99 | _ => 0, 100 | A_ => 0x41, 101 | _A => -0x41, 102 | ); 103 | 104 | $code = 'int exp, res;'; 105 | foreach (sort keys %tests) { 106 | my ($left, $right) = split '_', $_; 107 | $code .= "res = ft_strcmp(\"$left\", \"$right\"), exp = $tests{$_};\n"; 108 | $code .= "printf(\"ft_strcmp('$left', '$right') (%d vs %d) -> %d\\n\", exp, res, res == exp);\n"; 109 | } 110 | ==== check -l=6 ==== 111 | ==== 112 | 113 | 114 | ex07 115 | int ft_strncmp(char* s1, char* s2, unsigned int n) 116 | main -p -m ==== 117 | my %tests = ( 118 | asdf_asdf_4 => 0, 119 | asde_asdf_4 => -1, 120 | asdg_asdf_4 => 1, 121 | asdf_asdf_3 => 0, 122 | asde_asdf_3 => 0, 123 | __0 => 0, 124 | ); 125 | $code = 'int res, exp;'; 126 | foreach (sort keys %tests) { 127 | my ($left, $right, $len) = split _ => $_; 128 | $code .= "res = ft_strncmp(\"$left\", \"$right\", $len), exp = $tests{$_};\n"; 129 | $code .= "printf(\"ft_strncmp('$left', '$right', $len) (%d vs %d) -> %d\\n\", exp, res, res == exp);\n"; 130 | } 131 | ==== check -l=6 ==== 132 | ==== 133 | 134 | 135 | ex08 136 | char* ft_strupcase(char* str) 137 | main -m ==== 138 | char str[] = "asdf qWeRtY ZXCV"; 139 | printf("%s", ft_strupcase(str)); 140 | ==== check -e ==== 141 | $expected = 'ASDF QWERTY ZXCV'; 142 | ==== 143 | 144 | 145 | ex09 146 | char* ft_strlowcase(char* str) 147 | main -m ==== 148 | char str[] = "asdf qWeRtY ZXCV"; 149 | printf("%s", ft_strlowcase(str)); 150 | ==== check -e ==== 151 | $expected = 'asdf qwerty zxcv'; 152 | ==== 153 | 154 | ex10 155 | char* ft_strcapitalize(char* str) 156 | main -m ==== 157 | char str[] = "asdf qWeRtY ZXCV 100TIS\n"; 158 | printf("%s", ft_strcapitalize(str)); 159 | char str2[] = "asdf-qWeRtY ZXCV 100TIS"; 160 | printf("%s", ft_strcapitalize(str2)); 161 | ==== check -e ==== 162 | $expected = "Asdf Qwerty Zxcv 100tis\nAsdf-Qwerty Zxcv 100tis"; 163 | ==== 164 | 165 | ex11 166 | int ft_str_is_alpha(char* str) 167 | main -p -m ==== 168 | my %tests = ( 169 | asdf => 1, 170 | QWERTY => 1, 171 | asdf1234 => 0, 172 | '999' => 0, 173 | '' => 1, 174 | ); 175 | $code = join '', map "TEST(ft_str_is_alpha(\"$_\"), $tests{$_});\n", sort keys %tests; 176 | ==== check -l=5 ==== 177 | ==== 178 | 179 | 180 | ex12 181 | int ft_str_is_numeric(char* str) 182 | main -p -m ==== 183 | my %tests = ( 184 | 123456 => 1, 185 | asdf1234 => 0, 186 | 0 => 1, 187 | '' => 1, 188 | '12345asdf' => 0, 189 | ); 190 | $code = join '', map "TEST(ft_str_is_numeric(\"$_\"), $tests{$_});\n", sort keys %tests; 191 | ==== check -l=5 ==== 192 | ==== 193 | 194 | 195 | ex13 196 | int ft_str_is_lowercase(char* str) 197 | main -p -m ==== 198 | my %tests = ( 199 | asdf => 1, 200 | asdF => 0, 201 | ASDF => 0, 202 | 1234 => 0, 203 | '' => 1, 204 | ); 205 | $code = join '', map "TEST(ft_str_is_lowercase(\"$_\"), $tests{$_});\n", sort keys %tests; 206 | ==== check -l=5 ==== 207 | ==== 208 | 209 | 210 | ex14 211 | int ft_str_is_uppercase(char* str) 212 | main -p -m ==== 213 | my %tests = ( 214 | ASDF => 1, 215 | ASDf => 0, 216 | asdf => 0, 217 | 1234 => 0, 218 | '' => 1, 219 | ); 220 | $code = join '', map "TEST(ft_str_is_uppercase(\"$_\"), $tests{$_});\n", sort keys %tests; 221 | ==== check -l=5 ==== 222 | ==== 223 | 224 | 225 | ex15 226 | int ft_str_is_printable(char* str) 227 | main -p -m ==== 228 | my %tests = ( 229 | asdf => 1, 230 | 1234 => 1, 231 | ASDF => 1, 232 | '!@#$^&*()_+-=[]{}:;,./<>?' => 1, 233 | "\\xf0" => 0, 234 | "\\x7f" => 0, 235 | "\\n" => 0, 236 | '' => 1, 237 | ); 238 | $code = join '', map "TEST(ft_str_is_printable(\"$_\"), $tests{$_});\n", sort keys %tests; 239 | ==== check -l=8 ==== 240 | ==== 241 | 242 | 243 | ex16 244 | char* ft_strcat(char* dest, char* src) 245 | main -m ==== 246 | char test[256] = ""; 247 | printf("%s\n", ft_strcat(test, "asdf")); 248 | printf("%s\n", ft_strcat(test, "")); 249 | printf("%s\n", ft_strcat(test, "zxcv")); 250 | ==== check -e ==== 251 | $expected = "asdf\nasdf\nasdfzxcv\n"; 252 | ==== 253 | 254 | 255 | ex17 256 | char* ft_strncat(char* dest, char* src, int nb) 257 | main -m ==== 258 | char test[256] = "\0zxcvzxcvzxcvxzcvzxcvzxcv"; 259 | printf("%s\n", ft_strncat(test, "asdf", 16)); 260 | printf("%s\n", ft_strncat(test, "", 16)); 261 | printf("%s\n", ft_strncat(test, "qwerty", 0)); 262 | printf("%s\n", ft_strncat(test, "asdf", 3)); 263 | ==== check -e ==== 264 | $expected = "asdf\nasdf\nasdf\nasdfasd\n"; 265 | ==== 266 | 267 | 268 | ex18 269 | unsigned int ft_strlcat(char* dest, char* src, unsigned int size) 270 | main -m ==== 271 | char test[256] = "\0zxcvzxcvzxcvxzcvzxcv"; 272 | printf("%d-", ft_strlcat(test, "asdf", 16)); 273 | printf("%s\n", test); 274 | printf("%d-", ft_strlcat(test, "asdf", 6)); 275 | printf("%s\n", test); 276 | printf("%d-", ft_strlcat(test, "asdf", 4)); 277 | printf("%s\n", test); 278 | printf("%d-", ft_strlcat(test, "", 16)); 279 | printf("%s\n", test); 280 | printf("%d-", ft_strlcat(test, "asdf", 0)); 281 | printf("%s\n", test); 282 | ==== check -e ==== 283 | $expected = "4-asdf\n8-asdfa\n8-asdfa\n5-asdfa\n4-asdfa\n"; 284 | ==== 285 | 286 | 287 | ex19 288 | unsigned int ft_strlcpy(char* dest, char* src, unsigned int size) 289 | main -m ==== 290 | char test[256] = "\0zxcvzxcvzxcvxzcvzxcv"; 291 | printf("%d-", ft_strlcpy(test, "asdf", 16)); 292 | printf("%s\n", test); 293 | printf("%d-", ft_strlcpy(test, "uiop", 0)); 294 | printf("%s\n", test); 295 | printf("%d-", ft_strlcpy(test, "qwerty", 4)); 296 | printf("%s\n", test); 297 | printf("%d-", ft_strlcpy(test, "", 4)); 298 | printf("%s\n", test); 299 | 300 | ==== check -e ==== 301 | $expected = "4-asdf\n4-asdf\n6-qwe\n0-\n"; 302 | ==== 303 | 304 | 305 | 306 | ex20 307 | ft_putnbr_base(int nbr, char* base) 308 | main_basic -m ==== 309 | ft_putnbr_base(40, "0123456789abcdef"); 310 | ==== check_basic -e ==== 311 | $expected = "28"; 312 | ==== main_basic2 -m ==== 313 | ft_putnbr_base(31, "0123456789abcdef"); 314 | ==== check_basic2 -e ==== 315 | $expected = "1f"; 316 | ==== main_binary -m ==== 317 | ft_putnbr_base(15, "01"); 318 | ==== check_binary -e ==== 319 | $expected = '1111'; 320 | ==== main_negative -m ==== 321 | ft_putnbr_base(-15, "0123456789"); 322 | ft_putnbr_base(-16, "01"); 323 | ==== check_negative -e ==== 324 | $expected = '-15-10000'; 325 | ==== main_intmax -m ==== 326 | ft_putnbr_base(2147483647, "0123456789abcdef"); 327 | ft_putnbr_base(-2147483648, "0123456789abcdef"); 328 | ==== check_intmax -e ==== 329 | $expected = '7fffffff-80000000'; 330 | ==== 331 | 332 | 333 | ex21 334 | int ft_atoi_base(char* str, char* base) 335 | main -p -m ==== 336 | my %tests = ( 337 | _15_0123456789 => 15, 338 | _3f_0123456789abcdef => 63, 339 | _a_0a => 1, 340 | '_-15_0123456789' => -15, 341 | '_-111_01' => -7, 342 | 343 | _a_0 => 0, 344 | _1_012341234 => 0, 345 | '_1_01234+' => 0, 346 | _5_01234 => 0, 347 | __01234 => 0, 348 | ); 349 | foreach (sort keys %tests) { 350 | my (undef, $str, $base) = split '_'; 351 | $code .= "TEST(ft_atoi_base(\"$str\", \"$base\"), $tests{$_});\n"; 352 | } 353 | ==== check -l=10 ==== 354 | ==== 355 | 356 | 357 | ex22 358 | ft_putstr_non_printable(char* str) 359 | main -m ==== 360 | ft_putstr_non_printable("asdf\x7f\x1fhi\x01\xfflol"); 361 | ==== check -e ==== 362 | $expected = 'asdf\\7f\\1fhi\\01\\fflol'; 363 | ==== 364 | 365 | 366 | ex23 367 | void* ft_print_memory(void* addr, unsigned int size) 368 | main -m ==== 369 | ft_print_memory("asdfasdfqwertytyzxcvzxcv\0\0\xff\x7f\x01", 29); 370 | ==== check -e ==== 371 | $expected = 372 | '00000000: 6173 6466 6173 6466 7177 6572 7479 7479 asdfasdfqwertyty 373 | 00000010: 7a78 6376 7a78 6376 0000 ff7f 01 zxcvzxcv..... 374 | '; 375 | ==== 376 | 377 | 378 | 379 | 380 | 381 | -------------------------------------------------------------------------------- /spawn.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | use strict; 3 | use warnings; 4 | 5 | use feature 'say'; 6 | 7 | 8 | 9 | sub slurp_file { 10 | my ($file) = @_; 11 | 12 | warn "error opening file $file" and return unless open my $f, '<', $file; 13 | local $/; 14 | my $contents = <$f>; 15 | $f->close; 16 | 17 | return $contents 18 | } 19 | 20 | sub dump_file { 21 | my ($file, $contents) = @_; 22 | 23 | # warn "creating file $file\n"; 24 | 25 | open my $f, '>', $file; 26 | $f->print($contents); 27 | $f->close; 28 | } 29 | 30 | sub append_file { 31 | my ($file, $contents) = @_; 32 | 33 | # warn "appending file $file\n"; 34 | 35 | open my $f, '>>', $file; 36 | $f->print($contents); 37 | $f->close; 38 | } 39 | 40 | sub mirror_file { 41 | my ($file, $destination) = @_; 42 | my $contents = slurp_file($file); 43 | dump_file($destination, $contents); 44 | } 45 | 46 | sub get_given_function_prototype { 47 | my ($function, $content) = @_; 48 | 49 | my @protos = split /\r?\n/, $content; 50 | foreach my $proto (@protos) { 51 | if ($proto =~ /\A.+\s+\**(\w+)\(.*\);?\Z/) { 52 | return $proto if $1 eq $function; 53 | } 54 | } 55 | warn "prototype not found for function $function"; 56 | return 57 | } 58 | 59 | sub parse_function_ref { 60 | my ($function) = @_; 61 | 62 | my ($function_name, $function_proto); 63 | if ($function =~ /\A(\w+)\Z/) { 64 | $function_name = $1; 65 | $function_proto = "void $function_name(void)"; 66 | } elsif ($function =~ /\A(\w+)\((.*)\)\Z/) { 67 | $function_name = $1; 68 | $function_proto = "void $function_name($2)"; 69 | } elsif ($function =~ /\A(.*?) (\w+)\((.*)\)\Z/) { 70 | $function_name = $2; 71 | $function_proto = "$1 $function_name($3)"; 72 | } else { 73 | die "invalid function name: $function"; 74 | } 75 | 76 | return $function_name, $function_proto 77 | } 78 | 79 | sub read_file_from_config { 80 | my ($config, $break) = @_; 81 | $break = quotemeta $break; 82 | my $result = ''; 83 | until ($config->[0] =~ /\A$break(?:\s+(.+))?\Z/) { 84 | die "end of file while looking for break: $break" unless @$config; 85 | $result .= shift @$config; 86 | $result .= "\n"; 87 | } 88 | 89 | if ($config->[0] =~ /\A$break(?:\s+(.+))\Z/) { 90 | $config->[0] = $1; 91 | } else { 92 | shift @$config; 93 | } 94 | 95 | return $result 96 | } 97 | 98 | sub parse_flags { 99 | my ($flags) = @_; 100 | return map { 101 | if (/\A(\w+)=(.*)\Z/) { 102 | $1 => $2 103 | } else { 104 | $_ => 1 105 | } 106 | } map s/\A-//r, split ' ', $flags 107 | } 108 | 109 | sub spawn_source_file { 110 | my ($exercise, $exercise_flags, $name, $contents, $flags) = @_; 111 | 112 | dump_file("work/$exercise/$name.c", $contents); 113 | 114 | return "work/$exercise/$name.c" 115 | } 116 | 117 | sub spawn_header_file { 118 | my ($exercise, $exercise_flags, $name, $contents, $flags) = @_; 119 | 120 | dump_file("work/$exercise/$name.h", $contents); 121 | 122 | return "work/$exercise/$name.h" 123 | } 124 | 125 | sub spawn_main_file { 126 | my ($exercise, $exercise_flags, $name, $contents, $flags) = @_; 127 | 128 | my $prefix = "\n\n"; 129 | my $suffix = "\n\n"; 130 | 131 | $prefix .= " 132 | #include 133 | #include 134 | #include 135 | 136 | #define TEST(EXP, RES) printf(#EXP \" (\" #RES \" vs %d) -> %d\\n\", (EXP), ((EXP)) == (RES)) 137 | #define TEST_STR(EXP, RES) printf(#EXP \" (\" #RES \" vs \\\"%s\\\") -> %d\\n\", (EXP), strcmp(((EXP)), (RES)) == 0) 138 | 139 | "; 140 | 141 | if ($flags->{p}) { 142 | $contents = eval "my \$code = ''; $contents; return \$code"; 143 | die "error compiling main -p: $@" if $@; 144 | } 145 | 146 | if ($flags->{m}) { 147 | $prefix .= "\nint main()\n{\n"; 148 | $suffix .= "\nreturn 0;\n}\n"; 149 | } 150 | 151 | dump_file("work/$exercise/$name.c", "$prefix$contents$suffix"); 152 | 153 | my $other_files = join ' ', map "work/$exercise/$_", map $flags->{$_}, sort grep /\Af\d*\Z/, keys %$flags; 154 | 155 | append_file('tools/build.sh', " 156 | echo building work/$exercise/$name 157 | gcc -Wall -Wextra -Werror stupidity.c $other_files work/$exercise/$name.c -o work/$exercise/$name 158 | "); 159 | 160 | 161 | return "work/$exercise/$name.c" 162 | } 163 | 164 | sub spawn_check_file { 165 | my ($exercise, $exercise_flags, $name, $contents, $flags) = @_; 166 | 167 | 168 | my $main; 169 | if ($flags->{m}) { 170 | $main = "work/$exercise/$flags->{m}"; 171 | } else { 172 | $main = $exercise_flags->{__LAST_MAIN} // die "check file specified without main!"; 173 | } 174 | my $prefix = "\n\n"; 175 | my $suffix = "\n\n"; 176 | 177 | $prefix .= " 178 | my \$output = `./$main`; 179 | my \$expected; 180 | die \"$main failed to run: \$?\" if \$?; 181 | " unless $flags->{t}; 182 | 183 | 184 | $prefix .= " 185 | my \$count_lines = 0; 186 | my \$errors = 0; 187 | foreach my \$line (grep / -> [01]\\Z/, split /\\n/, \$output) { 188 | \$count_lines++; 189 | if (\$line !~ / -> 1\\Z/) { 190 | say \"!!!! ERROR in $main (line \$count_lines): '\$line'\"; 191 | \$errors++; 192 | } 193 | # debug 194 | # else { say \"passing: '\$line'\"; } 195 | } 196 | if (\$count_lines < $flags->{l}) { 197 | say \"!!!! ERROR in $main: expected $flags->{l} lines, got \$count_lines\"; 198 | } elsif (\$errors == 0) { 199 | say '$main good!'; 200 | } 201 | " if $flags->{l}; 202 | 203 | $prefix .= "my %tests;\n" if $flags->{t}; 204 | $suffix .= " 205 | my \$errors = 0; 206 | foreach my \$test (sort keys \%tests) { 207 | my \$output = `\$test`; 208 | my \$expected = \$tests{\$test}; 209 | if (\$output ne \$expected) { 210 | say \"!!!! ERROR in $main test `\$test`: '\$output'\"; 211 | say \"!!!! EXPECTED: '\$expected'\" if defined \$expected; 212 | \$errors++; 213 | } 214 | # else { say \"debug good: \$output\"; } 215 | } 216 | if (\$errors == 0) { 217 | say '$main good!'; 218 | } 219 | " if $flags->{t}; 220 | 221 | $suffix .= " 222 | if (\$output eq \$expected) { 223 | say '$main good!'; 224 | } else { 225 | say \"!!!! ERROR in $main: '\$output'\"; 226 | say \"!!!! EXPECTED: '\$expected'\" if defined \$expected; 227 | } 228 | " if $flags->{e}; 229 | 230 | dump_file("work/$exercise/$name.pl", "#!/usr/bin/env perl 231 | use strict; 232 | use warnings; 233 | use feature 'say'; 234 | 235 | my \$program = './$main'; 236 | 237 | $prefix 238 | $contents 239 | $suffix 240 | "); 241 | append_file('tools/build.sh', " 242 | cd work/$exercise; make; cd ../..; 243 | "); 244 | append_file('tools/check_all.sh', " 245 | perl work/$exercise/$name.pl 246 | "); 247 | return "work/$exercise/$name.pl" 248 | } 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | sub main { 258 | my ($project_directory, $config_file) = @_; 259 | 260 | die "project_directory required" unless defined $project_directory; 261 | die "config file required" unless defined $config_file; 262 | 263 | mkdir 'tools'; 264 | mkdir 'work'; 265 | 266 | dump_file('tools/build.sh', "#!/bin/sh\n\n"); 267 | dump_file('tools/verify.sh', "#!/bin/sh\n\n norminette -R CheckForbiddenSourceHeader"); 268 | dump_file('tools/check_all.sh', "#!/bin/sh\n\n"); 269 | 270 | chmod 0755, 'tools/build.sh', 'tools/verify.sh', 'tools/check_all.sh'; 271 | 272 | my @config = grep $_ ne '', split /\r?\n/, slurp_file($config_file); 273 | 274 | EXERCISE: while (@config) { 275 | my $thing = shift @config; 276 | if ($thing =~ /\A((?:main|check)\w*)(?: (-\w+(?:=\S+)?(?: -\w+(?:=\S+)?)*))? (=.*=)\Z/) { 277 | (undef) = read_file_from_config(\@config, $3); 278 | next; 279 | } 280 | my $exercise = $thing; 281 | my $function = shift @config; 282 | die "invalid exercise name $exercise" unless $exercise =~ /\A(\S+)(?: (-\w+(?:=\S+)?(?: -\w+(?:=\S+)?)*))?\Z/; 283 | $exercise = $1; 284 | my %exercise_flags; 285 | %exercise_flags = parse_flags($2) if defined $2; 286 | 287 | if ($exercise_flags{N}) { 288 | 289 | warn "\npreparing $exercise\n"; 290 | 291 | my @exercise_files = map $exercise_flags{$_}, sort grep /\Af\d*\Z/, keys %exercise_flags; 292 | my @exercise_globs = map $exercise_flags{$_}, sort grep /\AF\d*\Z/, keys %exercise_flags; 293 | unless (-e -d "$project_directory/$exercise") { 294 | warn "missing directory $project_directory/$exercise, skipping..."; 295 | next; 296 | } 297 | foreach my $file (@exercise_files) { 298 | unless (-e -f "$project_directory/$exercise/$file") { 299 | warn "missing project file $project_directory/$exercise/$file, skipping..."; 300 | next EXERCISE; 301 | } 302 | } 303 | 304 | mkdir "work/$exercise"; 305 | 306 | foreach my $glob (@exercise_globs) { 307 | foreach my $file_path (<$project_directory/$exercise/$glob>) { 308 | my $file = $file_path =~ s/\A.*\/([^\/]+)\Z/$1/r; 309 | warn "mirroring into work/$exercise/$file\n"; 310 | append_file('tools/verify.sh', " work/$exercise/$file") if $file =~ /\.[hc]\Z/; 311 | mirror_file("$file_path", "work/$exercise/$file"); 312 | } 313 | } 314 | foreach my $file (@exercise_files) { 315 | warn "mirroring into work/$exercise/$file\n"; 316 | append_file('tools/verify.sh', " work/$exercise/$file") if $file =~ /\.[hc]\Z/; 317 | mirror_file("$project_directory/$exercise/$file", "work/$exercise/$file"); 318 | } 319 | 320 | while (@config and $config[0] =~ /\A(?:(check|source|header|main)(?:_(\w+))?)(?: (-\w+(?:=\S+)?(?: -\w+(?:=\S+)?)*))? (=.*=)\Z/) { 321 | shift @config; 322 | 323 | my $spawn_type = $1; 324 | my $spawn_name = defined $2 ? $2 : $1; 325 | my %spawn_flags; 326 | my $break = $4; 327 | %spawn_flags = parse_flags($3) if defined $3; 328 | 329 | my $spawn_contents = read_file_from_config(\@config, $break); 330 | 331 | my $file; 332 | if ($spawn_type eq 'source') { 333 | $file = spawn_source_file($exercise, \%exercise_flags, $spawn_name, $spawn_contents, \%spawn_flags); 334 | } elsif ($spawn_type eq 'header') { 335 | $file = spawn_header_file($exercise, \%exercise_flags, $spawn_name, $spawn_contents, \%spawn_flags); 336 | } elsif ($spawn_type eq 'main') { 337 | $file = spawn_main_file($exercise, \%exercise_flags, $spawn_name, $spawn_contents, \%spawn_flags); 338 | $exercise_flags{__LAST_MAIN} = "work/$exercise/$spawn_name"; 339 | } else { 340 | $file = spawn_check_file($exercise, \%exercise_flags, $spawn_name, $spawn_contents, \%spawn_flags); 341 | } 342 | warn "$spawn_name at $file\n"; 343 | } 344 | 345 | } elsif ($exercise_flags{p}) { 346 | my $program_name = $function; 347 | 348 | warn "\npreparing $exercise/$program_name\n"; 349 | 350 | unless (-e -d "$project_directory/$exercise") { 351 | warn "missing directory $project_directory/$exercise, skipping..."; 352 | next; 353 | } 354 | unless (-e -f "$project_directory/$exercise/$program_name.c") { 355 | warn "missing project file $project_directory/$exercise/$program_name.c, skipping..."; 356 | next; 357 | } 358 | 359 | mkdir "work/$exercise"; 360 | 361 | mirror_file("$project_directory/$exercise/$program_name.c", "work/$exercise/$program_name.c"); 362 | 363 | append_file('tools/verify.sh', " work/$exercise/$program_name.c"); 364 | append_file('tools/build.sh', " 365 | echo building work/$exercise/$program_name 366 | gcc -Wall -Wextra -Werror stupidity.c work/$exercise/$program_name.c -o work/$exercise/$program_name 367 | "); 368 | 369 | while (@config and $config[0] =~ /\A(check\w*)(?: (-\w(?:=\S+)?(?: -\w(?:=\S+)?)*))? (=.*=)\Z/) { 370 | shift @config; 371 | my $check_file = "$1.pl"; 372 | my %check_flags; 373 | %check_flags = parse_flags($2) if defined $2; 374 | my $break = $3; 375 | warn "$check_file at work/$exercise/$check_file\n"; 376 | 377 | my $prefix = "\n\n"; 378 | my $suffix = "\n\n"; 379 | my $contents = read_file_from_config(\@config, $break); 380 | 381 | $prefix .= "my %tests;\n" if $check_flags{t}; 382 | $suffix .= " 383 | my \$errors = 0; 384 | foreach my \$test (sort keys \%tests) { 385 | \$output = `\$test`; 386 | \$expected = \$tests{\$test}; 387 | if (\$output ne \$expected) { 388 | say \"!!!! ERROR in work/$exercise/$check_file test `\$test`: '\$output'\"; 389 | say \"!!!! EXPECTED: '\$expected'\" if defined \$expected; 390 | \$errors++; 391 | } 392 | # else { say \"debug good: \$output\"; } 393 | } 394 | if (\$errors == 0) { 395 | say 'work/$exercise/$check_file good!'; 396 | } 397 | " if $check_flags{t}; 398 | 399 | $suffix .= " 400 | if (\$output eq \$expected) { 401 | say 'work/$exercise/$check_file good!'; 402 | } else { 403 | say \"!!!! ERROR in work/$exercise/$check_file: '\$output'\"; 404 | say \"!!!! EXPECTED: '\$expected'\" if defined \$expected; 405 | } 406 | " if $check_flags{e}; 407 | 408 | dump_file("work/$exercise/$check_file", "#!/usr/bin/env perl 409 | use strict; 410 | use warnings; 411 | use feature 'say'; 412 | 413 | my \$program = './work/$exercise/$program_name'; 414 | my \$output; 415 | my \$expected; 416 | 417 | $prefix 418 | $contents 419 | $suffix 420 | "); 421 | append_file('tools/check_all.sh', " 422 | perl work/$exercise/$check_file 423 | "); 424 | } 425 | 426 | } else { 427 | 428 | my ($function_name, $function_proto) = parse_function_ref($function); 429 | 430 | warn "\npreparing $exercise/$function_name\n"; 431 | 432 | unless (-e -d "$project_directory/$exercise") { 433 | warn "missing directory $project_directory/$exercise, skipping..."; 434 | next; 435 | } 436 | unless (-e -f "$project_directory/$exercise/$function_name.c") { 437 | warn "missing project file $project_directory/$exercise/$function_name.c, skipping..."; 438 | next; 439 | } 440 | my $exercise_contents = slurp_file("$project_directory/$exercise/$function_name.c"); 441 | my $import_proto = get_given_function_prototype($function_name, $exercise_contents); 442 | unless (defined $import_proto) { 443 | warn "missing prototype $function_name, skipping..."; 444 | next; 445 | } 446 | 447 | mkdir "work/$exercise"; 448 | 449 | warn "mirroring into work/$exercise/$function_name.c\n"; 450 | mirror_file("$project_directory/$exercise/$function_name.c", "work/$exercise/$function_name.c"); 451 | 452 | append_file('tools/verify.sh', " work/$exercise/$function_name.c"); 453 | 454 | while (@config and $config[0] =~ /\A(main\w*)(?: (-\w(?:=\S+)?(?: -\w(?:=\S+)?)*))? (=.*=)\Z/) { 455 | shift @config; 456 | my $main_file = $1; 457 | my %main_flags; 458 | %main_flags = parse_flags($2) if defined $2; 459 | my $contents = read_file_from_config(\@config, $3); 460 | warn "$main_file at work/$exercise/$main_file.c\n"; 461 | 462 | my $prefix = " 463 | #include 464 | #include 465 | #include 466 | 467 | #define TEST(EXP, RES) printf(#EXP \" (\" #RES \" vs %d) -> %d\\n\", (EXP), ((EXP)) == (RES)) 468 | #define TEST_STR(EXP, RES) printf(#EXP \" (\" #RES \" vs \\\"%s\\\") -> %d\\n\", (EXP), strcmp(((EXP)), (RES)) == 0) 469 | 470 | $import_proto; 471 | "; 472 | 473 | my $suffix = "\n\n"; 474 | 475 | if ($main_flags{p}) { 476 | $contents = eval "my \$code = ''; $contents; return \$code"; 477 | die "error compiling main -p: $@" if $@; 478 | } 479 | 480 | if ($main_flags{m}) { 481 | $prefix .= "\nint main()\n{\n"; 482 | $suffix .= "\nreturn 0;\n}\n"; 483 | } 484 | dump_file("work/$exercise/$main_file.c", "$prefix$contents$suffix"); 485 | append_file('tools/build.sh', " 486 | echo building work/$exercise/$main_file 487 | gcc -Wall -Wextra -Werror stupidity.c work/$exercise/$function_name.c work/$exercise/$main_file.c -o work/$exercise/$main_file 488 | "); 489 | while (@config and $config[0] =~ /\A(check\w*)(?: (-\w(?:=\S+)?(?: -\w(?:=\S+)?)*))? (=.*=)\Z/) 490 | { 491 | shift @config; 492 | my $check_file = "$1.pl"; 493 | my %check_flags; 494 | %check_flags = parse_flags($2) if defined $2; 495 | my $break = $3; 496 | warn "$check_file at work/$exercise/$check_file\n"; 497 | 498 | my $prefix = "\n\n"; 499 | my $suffix = "\n\n"; 500 | my $contents = read_file_from_config(\@config, $break); 501 | 502 | $prefix .= " 503 | \$output = `\$program`; 504 | " unless $check_flags{t}; 505 | 506 | $prefix .= " 507 | my \$count_lines = 0; 508 | my \$errors = 0; 509 | foreach my \$line (grep / -> [01]\\Z/, split /\\n/, \$output) { 510 | \$count_lines++; 511 | if (\$line !~ / -> 1\\Z/) { 512 | say \"!!!! ERROR in work/$exercise/$main_file (line \$count_lines): '\$line'\"; 513 | \$errors++; 514 | } 515 | # debug 516 | # else { say \"passing: '\$line'\"; } 517 | } 518 | if (\$count_lines < $check_flags{l}) { 519 | say \"!!!! ERROR in work/$exercise/$main_file: expected $check_flags{l} lines, got \$count_lines\"; 520 | } elsif (\$errors == 0) { 521 | say 'work/$exercise/$main_file good!'; 522 | } 523 | " if $check_flags{l}; 524 | $suffix .= " 525 | if (\$output eq \$expected) { 526 | say 'work/$exercise/$main_file good!'; 527 | } else { 528 | say \"!!!! ERROR in work/$exercise/$main_file: '\$output'\"; 529 | say \"!!!! EXPECTED: '\$expected'\" if defined \$expected; 530 | } 531 | " if $check_flags{e}; 532 | $prefix .= "my %tests;\n" if $check_flags{t}; 533 | $suffix .= " 534 | my \$errors = 0; 535 | foreach my \$test (sort keys \%tests) { 536 | \$output = `\$test`; 537 | \$expected = \$tests{\$test}; 538 | if (\$output ne \$expected) { 539 | say \"!!!! ERROR in work/$exercise/$check_file test `\$test`: '\$output'\"; 540 | say \"!!!! EXPECTED: '\$expected'\" if defined \$expected; 541 | \$errors++; 542 | } 543 | # else { say \"debug good: \$output\"; } 544 | } 545 | if (\$errors == 0) { 546 | say 'work/$exercise/$check_file good!'; 547 | } 548 | " if $check_flags{t}; 549 | 550 | dump_file("work/$exercise/$check_file", "#!/usr/bin/env perl 551 | use strict; 552 | use warnings; 553 | use feature 'say'; 554 | 555 | my \$program = './work/$exercise/$main_file'; 556 | my \$output; 557 | my \$expected; 558 | die \"$exercise/$main_file failed to run: \$?\" if \$?; 559 | 560 | $prefix 561 | $contents 562 | $suffix 563 | "); 564 | append_file('tools/check_all.sh', " 565 | perl work/$exercise/$check_file 566 | "); 567 | } 568 | } 569 | } 570 | } 571 | 572 | } 573 | 574 | 575 | 576 | caller or main(@ARGV); 577 | 578 | -------------------------------------------------------------------------------- /config_d11.pl: -------------------------------------------------------------------------------- 1 | 2 | ex00 -N -f1=ft_create_elem.c -f2=ft_list.h 3 | lol hi 4 | main -f=ft_create_elem.c ==== 5 | #include "ft_list.h" 6 | 7 | t_list *ft_create_elem(void *data); 8 | 9 | int main() 10 | { 11 | t_list* item = ft_create_elem("asdf"); 12 | printf("%s\n", (char*)item->data); 13 | printf("%p\n", item->next); 14 | } 15 | ==== check -e ==== 16 | $expected = "asdf\n0x0\n"; 17 | ==== 18 | 19 | 20 | 21 | 22 | ex01 -N -f1=ft_list_push_back.c -f2=ft_list.h 23 | lol hi 24 | main -f=ft_list_push_back.c ==== 25 | #include "ft_list.h" 26 | 27 | void ft_list_push_back(t_list **begin_list, void *data); 28 | 29 | t_list* ft_create_elem(void* data) 30 | { 31 | t_list* item = malloc(sizeof(t_list)); 32 | item->next = NULL; 33 | item->data = data; 34 | return (item); 35 | } 36 | 37 | void crap_a_list(t_list* list) 38 | { 39 | for (; list != 0; list = list->next) 40 | printf("%s,", (char*)list->data); 41 | printf("\n"); 42 | } 43 | 44 | int main() 45 | { 46 | t_list* list = 0; 47 | ft_list_push_back(&list, "asdf"); 48 | crap_a_list(list); 49 | ft_list_push_back(&list, "qwer"); 50 | ft_list_push_back(&list, "zxcv"); 51 | crap_a_list(list); 52 | list = 0; 53 | ft_list_push_back(&list, "uiop"); 54 | ft_list_push_back(&list, "hjkl"); 55 | crap_a_list(list); 56 | } 57 | ==== check -e ==== 58 | $expected = "asdf,\nasdf,qwer,zxcv,\nuiop,hjkl,\n"; 59 | ==== 60 | 61 | 62 | 63 | ex02 -N -f1=ft_list_push_front.c -f2=ft_list.h 64 | lol hi 65 | main -f=ft_list_push_front.c ==== 66 | #include "ft_list.h" 67 | 68 | void ft_list_push_front(t_list **begin_list, void *data); 69 | 70 | t_list* ft_create_elem(void* data) 71 | { 72 | t_list* item = malloc(sizeof(t_list)); 73 | item->next = NULL; 74 | item->data = data; 75 | return (item); 76 | } 77 | 78 | void crap_a_list(t_list* list) 79 | { 80 | for (; list != 0; list = list->next) 81 | printf("%s,", (char*)list->data); 82 | printf("\n"); 83 | } 84 | 85 | int main() 86 | { 87 | t_list* list = 0; 88 | ft_list_push_front(&list, "asdf"); 89 | crap_a_list(list); 90 | ft_list_push_front(&list, "qwer"); 91 | ft_list_push_front(&list, "zxcv"); 92 | crap_a_list(list); 93 | list = 0; 94 | ft_list_push_front(&list, "uiop"); 95 | ft_list_push_front(&list, "hjkl"); 96 | crap_a_list(list); 97 | } 98 | ==== check -e ==== 99 | $expected = "asdf,\nzxcv,qwer,asdf,\nhjkl,uiop,\n"; 100 | ==== 101 | 102 | 103 | 104 | 105 | 106 | ex03 -N -f1=ft_list_size.c -f2=ft_list.h 107 | lol hi 108 | main -f=ft_list_size.c ==== 109 | #include "ft_list.h" 110 | 111 | int ft_list_size(t_list *begin_list); 112 | 113 | t_list* ft_create_elem(void* data) 114 | { 115 | t_list* item = malloc(sizeof(t_list)); 116 | item->next = NULL; 117 | item->data = data; 118 | return (item); 119 | } 120 | 121 | #define CE(data) ft_create_elem(data) 122 | 123 | void crap_a_list(t_list* list) 124 | { 125 | for (; list != 0; list = list->next) 126 | printf("%s,", (char*)list->data); 127 | printf("\n"); 128 | } 129 | 130 | int main() 131 | { 132 | t_list* list = 0; 133 | printf("%d\n", ft_list_size(list)); 134 | list = CE("asdf"); 135 | printf("%d\n", ft_list_size(list)); 136 | list->next = CE("qwer"); 137 | list->next->next = CE("zxcv"); 138 | printf("%d\n", ft_list_size(list)); 139 | list = CE("asdf"); 140 | list->next = CE("asdf"); 141 | list->next->next = CE("asdf"); 142 | list->next->next->next = CE("asdf"); 143 | printf("%d\n", ft_list_size(list)); 144 | list->next->next->next->next = CE("asdf"); 145 | printf("%d\n", ft_list_size(list)); 146 | } 147 | ==== check -e ==== 148 | $expected = "0\n1\n3\n4\n5\n"; 149 | ==== 150 | 151 | 152 | ex04 -N -f1=ft_list_last.c -f2=ft_list.h 153 | lol hi 154 | main -f=ft_list_last.c ==== 155 | #include "ft_list.h" 156 | 157 | t_list *ft_list_last(t_list *begin_list); 158 | 159 | t_list* ft_create_elem(void* data) 160 | { 161 | t_list* item = malloc(sizeof(t_list)); 162 | item->next = NULL; 163 | item->data = data; 164 | return (item); 165 | } 166 | 167 | #define CE(data) ft_create_elem(data) 168 | #define CL(list) crap_a_list(list) 169 | 170 | void crap_a_list(t_list* list) 171 | { 172 | for (; list != 0; list = list->next) 173 | printf("%s,", (char*)list->data); 174 | printf("\n"); 175 | } 176 | 177 | int main() 178 | { 179 | t_list* list = 0; 180 | printf("%p\n", ft_list_last(list)); 181 | list = CE("asdf"); 182 | printf("%s\n", (char*)(ft_list_last(list)->data)); 183 | list->next = CE("qwer"); 184 | list->next->next = CE("zxcv"); 185 | printf("%s\n", (char*)(ft_list_last(list)->data)); 186 | list = CE("asdf"); 187 | list->next = CE("qwer"); 188 | list->next->next = CE("zxcv"); 189 | list->next->next->next = CE("uiop"); 190 | printf("%s\n", (char*)(ft_list_last(list)->data)); 191 | list->next->next->next->next = CE("hjkl"); 192 | printf("%s\n", (char*)(ft_list_last(list)->data)); 193 | } 194 | ==== check -e ==== 195 | $expected = "0x0\nasdf\nzxcv\nuiop\nhjkl\n"; 196 | ==== 197 | 198 | 199 | 200 | ex05 -N -f1=ft_list_push_params.c -f2=ft_list.h 201 | lol hi 202 | main -f=ft_list_push_params.c ==== 203 | #include "ft_list.h" 204 | 205 | t_list *ft_list_push_params(int ac, char **av); 206 | 207 | t_list* ft_create_elem(void* data) 208 | { 209 | t_list* item = malloc(sizeof(t_list)); 210 | item->next = NULL; 211 | item->data = data; 212 | return (item); 213 | } 214 | 215 | #define CE(data) ft_create_elem(data) 216 | #define CL(list) crap_a_list(list) 217 | 218 | void crap_a_list(t_list* list) 219 | { 220 | for (; list != 0; list = list->next) 221 | printf("%s,", (char*)list->data); 222 | printf("\n"); 223 | } 224 | 225 | int main(int argc, char** argv) 226 | { 227 | t_list* list = ft_list_push_params(argc, argv); 228 | CL(list); 229 | } 230 | ==== check -t ==== 231 | %tests = ( 232 | "$program" => "$program,\n", 233 | "$program asdf" => "asdf,$program,\n", 234 | "$program asdf qwer" => "qwer,asdf,$program,\n", 235 | "$program asdf qwer zxcv" => "zxcv,qwer,asdf,$program,\n", 236 | "$program ''" => ",$program,\n", 237 | "$program 15 '' 25" => "25,,15,$program,\n", 238 | "$program wat" => "wat,$program,\n", 239 | ); 240 | ==== 241 | 242 | 243 | 244 | ex06 -N -f1=ft_list_clear.c -f2=ft_list.h 245 | lol hi 246 | main -f=ft_list_clear.c ==== 247 | #include "ft_list.h" 248 | 249 | void ft_list_clear(t_list **begin_list); 250 | 251 | t_list* ft_create_elem(void* data) 252 | { 253 | t_list* item = malloc(sizeof(t_list)); 254 | item->next = NULL; 255 | item->data = data; 256 | return (item); 257 | } 258 | 259 | #define CE(data) ft_create_elem(data) 260 | #define CL(list) crap_a_list(list) 261 | 262 | void crap_a_list(t_list* list) 263 | { 264 | for (; list != 0; list = list->next) 265 | printf("%s,", (char*)list->data); 266 | printf("\n"); 267 | } 268 | 269 | int compare_ints(const int* v1, const int* v2) 270 | { 271 | return *v1 - *v2; 272 | } 273 | 274 | int main() 275 | { 276 | t_list* l = CE("asdf"); 277 | l->next = CE("qwer"); 278 | l->next->next = CE("zxcv"); 279 | t_list* addresses[] = {l, l->next, l->next->next}; 280 | qsort(addresses, 3, sizeof(t_list*), (int(*)(const void*, const void*))compare_ints); 281 | 282 | ft_list_clear(&l); 283 | // verify that this is set to a null pointer now 284 | printf("is null? %p\n", l); 285 | 286 | // verify that you freed the addresses by checking with malloc 287 | // if you freed them correctly, malloc will reuse them for this second batch 288 | l = CE("asdf"); 289 | l->next = CE("qwer"); 290 | l->next->next = CE("zxcv"); 291 | t_list* addresses2[] = {l, l->next, l->next->next}; 292 | qsort(addresses2, 3, sizeof(t_list*), (int(*)(const void*, const void*))compare_ints); 293 | printf("address good? %d,%d,%d\n", addresses[0] == addresses[0], addresses[1] == addresses[1], addresses[2] == addresses[2]); 294 | } 295 | ==== check -e ==== 296 | $expected = "is null? 0x0\naddress good? 1,1,1\n"; 297 | ==== 298 | 299 | 300 | 301 | ex07 -N -f1=ft_list_at.c -f2=ft_list.h 302 | lol hi 303 | main -f=ft_list_at.c ==== 304 | #include "ft_list.h" 305 | 306 | t_list *ft_list_at(t_list *begin_list, unsigned int nbr); 307 | 308 | t_list* ft_create_elem(void* data) 309 | { 310 | t_list* item = malloc(sizeof(t_list)); 311 | item->next = NULL; 312 | item->data = data; 313 | return (item); 314 | } 315 | 316 | #define CE(data) ft_create_elem(data) 317 | #define CL(list) crap_a_list(list) 318 | 319 | void crap_a_list(t_list* list) 320 | { 321 | for (; list != 0; list = list->next) 322 | printf("%s,", (char*)list->data); 323 | printf("\n"); 324 | } 325 | 326 | int main() 327 | { 328 | t_list* list = 0; 329 | printf("%p\n", ft_list_at(list, 0)); 330 | printf("%p\n", ft_list_at(list, 1)); 331 | printf("%p\n", ft_list_at(list, 100)); 332 | list = CE("asdf"); 333 | printf("%s\n", (char*)(ft_list_at(list, 0)->data)); 334 | printf("%p\n", ft_list_at(list, 1)); 335 | list->next = CE("qwer"); 336 | list->next->next = CE("zxcv"); 337 | list->next->next->next = CE("uiop"); 338 | list->next->next->next->next = CE("hjkl"); 339 | printf("%s\n", (char*)(ft_list_at(list, 0)->data)); 340 | printf("%s\n", (char*)(ft_list_at(list, 1)->data)); 341 | printf("%s\n", (char*)(ft_list_at(list, 2)->data)); 342 | printf("%s\n", (char*)(ft_list_at(list, 3)->data)); 343 | printf("%s\n", (char*)(ft_list_at(list, 4)->data)); 344 | printf("%p\n", ft_list_at(list, 5)); 345 | printf("%p\n", ft_list_at(list, 100)); 346 | } 347 | ==== check -e ==== 348 | $expected = "0x0\n0x0\n0x0\nasdf\n0x0\nasdf\nqwer\nzxcv\nuiop\nhjkl\n0x0\n0x0\n"; 349 | ==== 350 | 351 | 352 | ex08 -N -f1=ft_list_reverse.c -f2=ft_list.h 353 | lol hi 354 | main -f=ft_list_reverse.c ==== 355 | #include "ft_list.h" 356 | 357 | void ft_list_reverse(t_list **begin_list); 358 | 359 | t_list* ft_create_elem(void* data) 360 | { 361 | t_list* item = malloc(sizeof(t_list)); 362 | item->next = NULL; 363 | item->data = data; 364 | return (item); 365 | } 366 | #define CE(data) ft_create_elem(data) 367 | #define CL(list) crap_a_list(list) 368 | 369 | // pronounced with an exclaimation point at the end 370 | void crap_a_list(t_list* list) 371 | { 372 | for (; list != 0; list = list->next) 373 | printf("%s,", (char*)list->data); 374 | printf("\n"); 375 | } 376 | 377 | // pronounced with an exclaimation point at the end 378 | t_list* crap_the_argv(char** argv) 379 | { 380 | t_list* res = 0; 381 | t_list* tail = 0; 382 | argv++; 383 | while (*argv != 0) 384 | { 385 | if (tail == 0) 386 | res = tail = CE(*argv++); 387 | else 388 | { 389 | tail->next = CE(*argv++); 390 | tail = tail->next; 391 | } 392 | } 393 | return res; 394 | } 395 | 396 | int main(int argc, char** argv) 397 | { 398 | if (argc < 1) 399 | return 1; 400 | t_list* list = crap_the_argv(argv); 401 | ft_list_reverse(&list); 402 | crap_a_list(list); 403 | } 404 | ==== check -t ==== 405 | %tests = ( 406 | "$program" => "\n", 407 | "$program asdf" => "asdf,\n", 408 | "$program asdf qwer" => "qwer,asdf,\n", 409 | "$program asdf qwer zxcv" => "zxcv,qwer,asdf,\n", 410 | "$program ''" => ",\n", 411 | "$program 15 '' 25" => "25,,15,\n", 412 | "$program wat" => "wat,\n", 413 | ); 414 | ==== 415 | 416 | 417 | 418 | ex09 -N -f1=ft_list_foreach.c -f2=ft_list.h 419 | lol hi 420 | main -f=ft_list_foreach.c ==== 421 | #include "ft_list.h" 422 | 423 | void ft_list_foreach(t_list *begin_list, void (*f)(void *)); 424 | 425 | t_list* ft_create_elem(void* data) 426 | { 427 | t_list* item = malloc(sizeof(t_list)); 428 | item->next = NULL; 429 | item->data = data; 430 | return (item); 431 | } 432 | #define CE(data) ft_create_elem(data) 433 | #define CL(list) crap_a_list(list) 434 | 435 | // pronounced with an exclaimation point at the end 436 | void crap_a_list(t_list* list) 437 | { 438 | for (; list != 0; list = list->next) 439 | printf("%s,", (char*)list->data); 440 | printf("\n"); 441 | } 442 | 443 | // pronounced with an exclaimation point at the end 444 | t_list* crap_the_argv(char** argv) 445 | { 446 | t_list* res = 0; 447 | t_list* tail = 0; 448 | argv++; 449 | while (*argv != 0) 450 | { 451 | if (tail == 0) 452 | res = tail = CE(*argv++); 453 | else 454 | { 455 | tail->next = CE(*argv++); 456 | tail = tail->next; 457 | } 458 | } 459 | return res; 460 | } 461 | 462 | void print_str(char* str) 463 | { 464 | printf("%s\n", str); 465 | } 466 | 467 | int main(int argc, char** argv) 468 | { 469 | if (argc < 1) 470 | return 1; 471 | t_list* list = crap_the_argv(argv); 472 | ft_list_foreach(list, (void(*)(void*))print_str); 473 | } 474 | ==== check -t ==== 475 | %tests = ( 476 | "$program" => "", 477 | "$program asdf" => "asdf\n", 478 | "$program asdf qwer" => "asdf\nqwer\n", 479 | "$program asdf qwer zxcv" => "asdf\nqwer\nzxcv\n", 480 | "$program ''" => "\n", 481 | "$program 15 '' 25" => "15\n\n25\n", 482 | "$program wat" => "wat\n", 483 | ); 484 | ==== 485 | 486 | 487 | 488 | ex10 -N -f1=ft_list_foreach_if.c -f2=ft_list.h 489 | lol hi 490 | main -f=ft_list_foreach_if.c ==== 491 | #include "ft_list.h" 492 | 493 | void ft_list_foreach_if(t_list *begin_list, void (*f)(void *), void *data_ref, int (*cmp)()); 494 | 495 | t_list* ft_create_elem(void* data) 496 | { 497 | t_list* item = malloc(sizeof(t_list)); 498 | item->next = NULL; 499 | item->data = data; 500 | return (item); 501 | } 502 | #define CE(data) ft_create_elem(data) 503 | #define CL(list) crap_a_list(list) 504 | 505 | // pronounced with an exclaimation point at the end 506 | void crap_a_list(t_list* list) 507 | { 508 | for (; list != 0; list = list->next) 509 | printf("%s,", (char*)list->data); 510 | printf("\n"); 511 | } 512 | 513 | // pronounced with an exclaimation point at the end 514 | t_list* crap_the_argv(char** argv) 515 | { 516 | t_list* res = 0; 517 | t_list* tail = 0; 518 | argv++; 519 | while (*argv != 0) 520 | { 521 | if (tail == 0) 522 | res = tail = CE(*argv++); 523 | else 524 | { 525 | tail->next = CE(*argv++); 526 | tail = tail->next; 527 | } 528 | } 529 | return res; 530 | } 531 | 532 | void print_str(char* str) 533 | { 534 | printf("%s\n", str); 535 | } 536 | 537 | int starts_with(char* str, char* ref) 538 | { 539 | return str[0] - ref[0]; 540 | } 541 | 542 | int main(int argc, char** argv) 543 | { 544 | if (argc < 1) 545 | return 1; 546 | t_list* list = crap_the_argv(argv); 547 | ft_list_foreach_if(list, (void(*)(void*))print_str, "a", (int(*)())starts_with); 548 | } 549 | ==== check -t ==== 550 | %tests = ( 551 | "$program" => "", 552 | "$program asdf" => "asdf\n", 553 | "$program asdf qwer" => "asdf\n", 554 | "$program asdf qwer zxcv" => "asdf\n", 555 | "$program ''" => "", 556 | "$program uiop aaaaa qwer alpha zxcv asdf hjkl" => "aaaaa\nalpha\nasdf\n", 557 | "$program wat atw taw awt twa" => "atw\nawt\n", 558 | ); 559 | ==== 560 | 561 | 562 | ex11 -N -f1=ft_list_find.c -f2=ft_list.h 563 | lol hi 564 | main -f=ft_list_find.c ==== 565 | #include "ft_list.h" 566 | 567 | t_list *ft_list_find(t_list *begin_list, void *data_ref, int (*cmp)()); 568 | 569 | t_list* ft_create_elem(void* data) 570 | { 571 | t_list* item = malloc(sizeof(t_list)); 572 | item->next = NULL; 573 | item->data = data; 574 | return (item); 575 | } 576 | #define CE(data) ft_create_elem(data) 577 | #define CL(list) crap_a_list(list) 578 | 579 | // pronounced with an exclaimation point at the end 580 | void crap_a_list(t_list* list) 581 | { 582 | for (; list != 0; list = list->next) 583 | printf("%s,", (char*)list->data); 584 | printf("\n"); 585 | } 586 | 587 | // pronounced with an exclaimation point at the end 588 | t_list* crap_the_argv(char** argv) 589 | { 590 | t_list* res = 0; 591 | t_list* tail = 0; 592 | argv++; 593 | while (*argv != 0) 594 | { 595 | if (tail == 0) 596 | res = tail = CE(*argv++); 597 | else 598 | { 599 | tail->next = CE(*argv++); 600 | tail = tail->next; 601 | } 602 | } 603 | return res; 604 | } 605 | 606 | void print_str(char* str) 607 | { 608 | printf("%s\n", str); 609 | } 610 | 611 | int starts_with(char* str, char* ref) 612 | { 613 | return str[0] - ref[0]; 614 | } 615 | 616 | int main(int argc, char** argv) 617 | { 618 | if (argc < 1) 619 | return 1; 620 | t_list* list = crap_the_argv(argv); 621 | t_list* res = ft_list_find(list, "a", (int(*)())starts_with); 622 | if (res == 0) 623 | printf("(NULL)"); 624 | else 625 | printf("%s", (char*)res->data); 626 | } 627 | ==== check -t ==== 628 | %tests = ( 629 | "$program" => "(NULL)", 630 | "$program asdf" => "asdf", 631 | "$program qwer" => "(NULL)", 632 | "$program qwer asdf zxcv" => "asdf", 633 | "$program ''" => "(NULL)", 634 | "$program uiop aaaaa qwer alpha zxcv asdf hjkl" => "aaaaa", 635 | "$program wat atw taw awt twa" => "atw", 636 | ); 637 | ==== 638 | 639 | 640 | 641 | 642 | 643 | 644 | ex12 -N -f1=ft_list_remove_if.c -f2=ft_list.h 645 | lol hi 646 | main -f=ft_list_remove_if.c ==== 647 | #include "ft_list.h" 648 | 649 | void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)()); 650 | 651 | t_list* ft_create_elem(void* data) 652 | { 653 | t_list* item = malloc(sizeof(t_list)); 654 | item->next = NULL; 655 | item->data = data; 656 | return (item); 657 | } 658 | #define CE(data) ft_create_elem(data) 659 | #define CL(list) crap_a_list(list) 660 | 661 | // pronounced with an exclaimation point at the end 662 | void crap_a_list(t_list* list) 663 | { 664 | for (; list != 0; list = list->next) 665 | printf("%s,", (char*)list->data); 666 | printf("\n"); 667 | } 668 | 669 | // pronounced with an exclaimation point at the end 670 | t_list* crap_the_argv(char** argv) 671 | { 672 | t_list* res = 0; 673 | t_list* tail = 0; 674 | argv++; 675 | while (*argv != 0) 676 | { 677 | if (tail == 0) 678 | res = tail = CE(*argv++); 679 | else 680 | { 681 | tail->next = CE(*argv++); 682 | tail = tail->next; 683 | } 684 | } 685 | return res; 686 | } 687 | 688 | void print_str(char* str) 689 | { 690 | printf("%s\n", str); 691 | } 692 | 693 | int starts_with(char* str, char* ref) 694 | { 695 | return str[0] - ref[0]; 696 | } 697 | 698 | int main(int argc, char** argv) 699 | { 700 | if (argc < 1) 701 | return 1; 702 | t_list* list = crap_the_argv(argv); 703 | ft_list_remove_if(&list, "a", (int(*)())starts_with); 704 | crap_a_list(list); 705 | } 706 | ==== check -t ==== 707 | %tests = ( 708 | "$program" => "\n", 709 | "$program asdf" => "\n", 710 | "$program qwer" => "qwer,\n", 711 | "$program qwer asdf zxcv" => "qwer,zxcv,\n", 712 | "$program ''" => ",\n", 713 | "$program uiop aaaaa qwer alpha zxcv asdf hjkl" => "uiop,qwer,zxcv,hjkl,\n", 714 | "$program wat atw taw awt twa" => "wat,taw,twa,\n", 715 | ); 716 | ==== 717 | 718 | 719 | 720 | ex13 -N -f1=ft_list_merge.c -f2=ft_list.h 721 | lol hi 722 | main -f=ft_list_merge.c ==== 723 | #include "ft_list.h" 724 | 725 | void ft_list_merge(t_list **begin_list1, t_list *begin_list2); 726 | 727 | t_list* ft_create_elem(void* data) 728 | { 729 | t_list* item = malloc(sizeof(t_list)); 730 | item->next = NULL; 731 | item->data = data; 732 | return (item); 733 | } 734 | #define CE(data) ft_create_elem(data) 735 | #define CL(list) crap_a_list(list) 736 | 737 | // pronounced with an exclaimation point at the end 738 | void crap_a_list(t_list* list) 739 | { 740 | for (; list != 0; list = list->next) 741 | printf("%s,", (char*)list->data); 742 | printf("\n"); 743 | } 744 | 745 | // pronounced with an exclaimation point at the end 746 | t_list* crap_the_argv(char** argv) 747 | { 748 | t_list* res = 0; 749 | t_list* tail = 0; 750 | argv++; 751 | while (*argv != 0) 752 | { 753 | if (tail == 0) 754 | res = tail = CE(*argv++); 755 | else 756 | { 757 | tail->next = CE(*argv++); 758 | tail = tail->next; 759 | } 760 | } 761 | return res; 762 | } 763 | 764 | void print_str(char* str) 765 | { 766 | printf("%s\n", str); 767 | } 768 | 769 | int starts_with(char* str, char* ref) 770 | { 771 | return str[0] - ref[0]; 772 | } 773 | 774 | int main(int argc, char** argv) 775 | { 776 | if (argc < 1) 777 | return 1; 778 | t_list* initial_list = 0; 779 | t_list* list = crap_the_argv(argv); 780 | ft_list_merge(&initial_list, list); 781 | crap_a_list(initial_list); 782 | 783 | 784 | initial_list = CE("first"); 785 | initial_list->next = CE("second"); 786 | list = crap_the_argv(argv); 787 | ft_list_merge(&initial_list, list); 788 | crap_a_list(initial_list); 789 | } 790 | ==== check -t ==== 791 | %tests = ( 792 | "$program" => "\nfirst,second,\n", 793 | "$program asdf" => "asdf,\nfirst,second,asdf,\n", 794 | "$program qwer asdf zxcv" => "qwer,asdf,zxcv,\nfirst,second,qwer,asdf,zxcv,\n", 795 | ); 796 | ==== 797 | 798 | 799 | 800 | ex14 -N -f1=ft_list_sort.c -f2=ft_list.h 801 | lol hi 802 | main -f=ft_list_sort.c ==== 803 | #include "ft_list.h" 804 | 805 | void ft_list_sort(t_list **begin_list, int (*cmp)()); 806 | 807 | t_list* ft_create_elem(void* data) 808 | { 809 | t_list* item = malloc(sizeof(t_list)); 810 | item->next = NULL; 811 | item->data = data; 812 | return (item); 813 | } 814 | #define CE(data) ft_create_elem(data) 815 | #define CL(list) crap_a_list(list) 816 | 817 | // pronounced with an exclaimation point at the end 818 | void crap_a_list(t_list* list) 819 | { 820 | for (; list != 0; list = list->next) 821 | printf("%s,", (char*)list->data); 822 | printf("\n"); 823 | } 824 | 825 | // pronounced with an exclaimation point at the end 826 | t_list* crap_the_argv(char** argv) 827 | { 828 | t_list* res = 0; 829 | t_list* tail = 0; 830 | argv++; 831 | while (*argv != 0) 832 | { 833 | if (tail == 0) 834 | res = tail = CE(*argv++); 835 | else 836 | { 837 | tail->next = CE(*argv++); 838 | tail = tail->next; 839 | } 840 | } 841 | return res; 842 | } 843 | 844 | void print_str(char* str) 845 | { 846 | printf("%s\n", str); 847 | } 848 | 849 | int starts_with(char* str, char* ref) 850 | { 851 | return str[0] - ref[0]; 852 | } 853 | 854 | int main(int argc, char** argv) 855 | { 856 | if (argc < 1) 857 | return 1; 858 | t_list* list = crap_the_argv(argv); 859 | ft_list_sort(&list, (int(*)())strcmp); 860 | crap_a_list(list); 861 | } 862 | ==== check -t ==== 863 | %tests = ( 864 | "$program" => "\n", 865 | "$program asdf" => "asdf,\n", 866 | "$program asdf zxcv" => "asdf,zxcv,\n", 867 | "$program zxcv asdf" => "asdf,zxcv,\n", 868 | "$program qwer asdf zxcv" => "asdf,qwer,zxcv,\n", 869 | "$program zxcv asdf qwer" => "asdf,qwer,zxcv,\n", 870 | "$program zxcv qwer asdf" => "asdf,qwer,zxcv,\n", 871 | "$program qwer zxcv asdf" => "asdf,qwer,zxcv,\n", 872 | "$program asdf qwer zxcv" => "asdf,qwer,zxcv,\n", 873 | "$program zxcv doop qwer asdf uiop hjkl vbnm" => "asdf,doop,hjkl,qwer,uiop,vbnm,zxcv,\n", 874 | ); 875 | ==== 876 | 877 | 878 | 879 | ex15 -N -f1=ft_list_reverse_fun.c -f2=ft_list.h 880 | lol hi 881 | main -f=ft_list_reverse_fun.c ==== 882 | #include "ft_list.h" 883 | 884 | void ft_list_reverse_fun(t_list *begin_list); 885 | 886 | t_list* ft_create_elem(void* data) 887 | { 888 | t_list* item = malloc(sizeof(t_list)); 889 | item->next = NULL; 890 | item->data = data; 891 | return (item); 892 | } 893 | #define CE(data) ft_create_elem(data) 894 | #define CL(list) crap_a_list(list) 895 | 896 | // pronounced with an exclaimation point at the end 897 | void crap_a_list(t_list* list) 898 | { 899 | for (; list != 0; list = list->next) 900 | printf("%s,", (char*)list->data); 901 | printf("\n"); 902 | } 903 | 904 | // pronounced with an exclaimation point at the end 905 | t_list* crap_the_argv(char** argv) 906 | { 907 | t_list* res = 0; 908 | t_list* tail = 0; 909 | argv++; 910 | while (*argv != 0) 911 | { 912 | if (tail == 0) 913 | res = tail = CE(*argv++); 914 | else 915 | { 916 | tail->next = CE(*argv++); 917 | tail = tail->next; 918 | } 919 | } 920 | return res; 921 | } 922 | 923 | void print_str(char* str) 924 | { 925 | printf("%s\n", str); 926 | } 927 | 928 | int starts_with(char* str, char* ref) 929 | { 930 | return str[0] - ref[0]; 931 | } 932 | 933 | int main(int argc, char** argv) 934 | { 935 | if (argc < 1) 936 | return 1; 937 | t_list* list = crap_the_argv(argv); 938 | ft_list_reverse_fun(list); 939 | crap_a_list(list); 940 | } 941 | ==== check -t ==== 942 | %tests = ( 943 | "$program" => "\n", 944 | "$program asdf" => "asdf,\n", 945 | "$program asdf zxcv" => "zxcv,asdf,\n", 946 | "$program zxcv asdf" => "asdf,zxcv,\n", 947 | "$program asdf qwer zxcv" => "zxcv,qwer,asdf,\n", 948 | "$program zxcv doop qwer asdf uiop hjkl vbnm" => "vbnm,hjkl,uiop,asdf,qwer,doop,zxcv,\n", 949 | ); 950 | ==== 951 | --------------------------------------------------------------------------------