From 7d7b644860bbd968fb14381f52913ddad6a9e0bd Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Sat, 18 Feb 2023 14:35:02 +0300 Subject: [PATCH] Make .c files really just DROP-IN; add a few new FS functions --- src/bits/bits.c | 4 +- src/crypt/xorcipher.c | 1 - src/datastruct/cvec.c | 10 ++- src/endian/endian.c | 4 +- src/fs/fs.c | 57 ++++++++++++-- src/fs/fs.h | 19 +++-- src/img/ppm.c | 35 +++++++-- src/img/ppm.h | 6 +- src/math/misc.c | 8 +- src/math/vector.c | 153 +++++++++++++++++++++++++++++++++++++- src/math/vector.h | 4 +- src/rng/rng.c | 2 +- src/strings/levenshtein.c | 14 +++- src/strings/levenshtein.h | 12 ++- testing/test.c | 15 +++- 15 files changed, 299 insertions(+), 45 deletions(-) diff --git a/src/bits/bits.c b/src/bits/bits.c index d9aff5d..6537590 100644 --- a/src/bits/bits.c +++ b/src/bits/bits.c @@ -1,7 +1,7 @@ /* The MIT License (MIT) -Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) +Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -10,7 +10,7 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "bits.h" +#include // Returns 1 if position bit in byte is 1, 0 if 0; -1 is returned in case position is not in range [1..8]. // Position is counted from the least significant bit, ie: in 10010010 position 1 is "0", but position 8 is "1" diff --git a/src/crypt/xorcipher.c b/src/crypt/xorcipher.c index d23fa72..857b2b3 100644 --- a/src/crypt/xorcipher.c +++ b/src/crypt/xorcipher.c @@ -10,7 +10,6 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "xorcipher.h" #include // XOR character with a key diff --git a/src/datastruct/cvec.c b/src/datastruct/cvec.c index 08482cc..93f3e2b 100644 --- a/src/datastruct/cvec.c +++ b/src/datastruct/cvec.c @@ -10,8 +10,16 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "cvec.h" #include +#include + +// Basic vector structure +typedef struct cvec { + size_t cap; + size_t size; + size_t data_size; + char* contents; +} cvec; // Create new vector with the maximum data elements of cap where data is data_size bytes long cvec cvec_new(size_t data_size, size_t cap) { diff --git a/src/endian/endian.c b/src/endian/endian.c index 0ff334a..2b0e6b1 100644 --- a/src/endian/endian.c +++ b/src/endian/endian.c @@ -1,7 +1,7 @@ /* The MIT License (MIT) -Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) +Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -10,7 +10,7 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "endian.h" +#include // Determines this machine's endianness. Returns 0 in case it is of Big-Endian type, 1 - Little-Endian int endianness() { diff --git a/src/fs/fs.c b/src/fs/fs.c index 2ddd6b0..aaf673c 100644 --- a/src/fs/fs.c +++ b/src/fs/fs.c @@ -1,7 +1,7 @@ /* The MIT License (MIT) -Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) +Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -10,7 +10,19 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "fs.h" +#include +#include +#include +#include +#include + +#if __WIN32__ +#include +#include +#define stat _stat +#else +#include +#endif // Get file size. returns 18446744073709551615 (uint64_t maximum value) if something went wrong uint64_t file_size(char* path) { @@ -23,7 +35,27 @@ uint64_t file_size(char* path) { return (uint64_t) file_stats.st_size; } -// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns -1 in case of an error and 0 if the operation was sucessfull +// Get file size by actually reading the whole file byte-by-byte until it hits EOF. Returns UINT64_MAX on open error +uint64_t file_size_brute(char* path) { + uint64_t size = 0; + int c = 0; + FILE* file = NULL; + + file = fopen(path, "rb"); + if (!file) { + return UINT64_MAX; + } + + while((c = getc(file)) != EOF) { + size++; + } + + fclose(file); + + return size; +} + +// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns 0 in case of an error and 1 if the operation was sucessfull int copy_file(char* path_src, char* path_dst) { FILE* src_file; FILE* dst_file; @@ -33,12 +65,12 @@ int copy_file(char* path_src, char* path_dst) { src_file = fopen(path_src, "rb"); if (!src_file) { - return -1; + return 0; } dst_file = fopen(path_dst, "wb"); if (!dst_file) { - return -1; + return 0; } while (!feof(src_file)) { @@ -49,5 +81,18 @@ int copy_file(char* path_src, char* path_dst) { fclose(src_file); fclose(dst_file); - return 0; + return 1; +} + +// Check whether a file can be opened in read mode. Returns 0 in case of an error, 1 otherwise (successfully opened file) +int can_open_file(char* path) { + FILE* file = NULL; + + file = fopen(path, "r"); + if (!file) { + return 0; + } + fclose(file); + + return 1; } \ No newline at end of file diff --git a/src/fs/fs.h b/src/fs/fs.h index 8befbc1..89ed097 100644 --- a/src/fs/fs.h +++ b/src/fs/fs.h @@ -1,7 +1,7 @@ /* The MIT License (MIT) -Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) +Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -10,17 +10,16 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include - -#if __WIN32__ -#define stat _stat64 -#endif // Get file size. returns 18446744073709551615 (uint64_t maximum value) if something went wrong uint64_t file_size(char* path); -// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns -1 in case of an error and 0 if the operation was sucessfull -int copy_file(char* path_src, char* path_dst); \ No newline at end of file +// Get file size by actually reading the whole file byte-by-byte until it hits EOF. Returns UINT64_MAX on open error +uint64_t file_size_brute(char* path); + +// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns 0 in case of an error and 1 if the operation was sucessfull +int copy_file(char* path_src, char* path_dst); + +// Check whether a file can be opened in read mode. Returns 0 in case of an error, 1 otherwise (successfully opened file) +int can_open_file(char* path); \ No newline at end of file diff --git a/src/img/ppm.c b/src/img/ppm.c index c373ce6..9524a1f 100644 --- a/src/img/ppm.c +++ b/src/img/ppm.c @@ -1,7 +1,7 @@ /* The MIT License (MIT) -Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) +Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -10,7 +10,26 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "ppm.h" +#include +#include +#include +#include +#include + +// 8bit Red-Green-Blue color representation +typedef struct rgb8 { + uint8_t r; + uint8_t g; + uint8_t b; +} rgb8; + +// Minimal ppm image structure +typedef struct ppm_image { + unsigned int width; + unsigned int height; + rgb8* pixels; +} ppm_image; + // Read ppm image from file on the disk. Returns NULL if something went wrong ppm_image* read_ppm(const char* path) { @@ -83,13 +102,13 @@ ppm_image* read_ppm(const char* path) { return ppm; } -// Write ppm image to the disk. Returns 0 if everything is alright, 1 otherwise +// Write ppm image to the disk. Returns 1 if everything is alright, 0 otherwise int write_ppm(const ppm_image* ppm, const char* path) { FILE* file; file = fopen(path, "w"); if (!file) { - return 1; + return 0; } fprintf(file, "P6\n"); @@ -98,21 +117,21 @@ int write_ppm(const ppm_image* ppm, const char* path) { fclose(file); - return 0; + return 1; } -// Put pixel with specified rgb8 color at x,y coordinates. Returns 0 if pixel has been replaced, 1 if +// Put pixel with specified rgb8 color at x,y coordinates. Returns 1 if pixel has been replaced, 0 if // coordinates are out of bounds int put_pixel_at(const unsigned int x, const unsigned int y, const rgb8 color, ppm_image* ppm) { unsigned int index = ppm->width * y + x; if (index >= ppm->width*ppm->height) { // out of bounds ! - return 1; + return 0; } ppm->pixels[index] = color; - return 0; + return 1; } // Get pixel color at specified coordinates. Returns a const pointer to that pixel color if diff --git a/src/img/ppm.h b/src/img/ppm.h index 2f338c9..8825c9e 100644 --- a/src/img/ppm.h +++ b/src/img/ppm.h @@ -1,7 +1,7 @@ /* The MIT License (MIT) -Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) +Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -34,10 +34,10 @@ typedef struct ppm_image { // Read ppm image from file on the disk. Returns NULL if something went wrong ppm_image* read_ppm(const char* path); -// Write ppm image to the disk. Returns 0 if everything is alright, 1 otherwise +// Write ppm image to the disk. Returns 1 if everything is alright, 0 otherwise int write_ppm(const ppm_image* ppm, const char* path); -// Put pixel with specified rgb8 color at x,y coordinates. Returns 0 if pixel has been replaced, 1 if +// Put pixel with specified rgb8 color at x,y coordinates. Returns 1 if pixel has been replaced, 0 if // coordinates are out of bounds int put_pixel_at(const unsigned int x, const unsigned int y, const rgb8 color, ppm_image* ppm); diff --git a/src/math/misc.c b/src/math/misc.c index b7f6a29..d36f14f 100644 --- a/src/math/misc.c +++ b/src/math/misc.c @@ -11,8 +11,6 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I */ // Returns an absolute value of n -#include "misc.h" - char absch(char n) { if (n >= 0) { return n; @@ -84,6 +82,12 @@ long double absld(long double n) { } } +// Returns the approximate result of base^exp +float powerf(float base, float exp); + +// Returns the approximate result of base^exp +long double powerl(long double base, long double exp); + long double n_root(long double a, unsigned int n, unsigned int max_iter, long double epsilon) { if (n == 1 || a == 0) { return a; diff --git a/src/math/vector.c b/src/math/vector.c index b916e7b..283256c 100644 --- a/src/math/vector.c +++ b/src/math/vector.c @@ -1,7 +1,7 @@ /* The MIT License (MIT) -Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) +Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -10,7 +10,156 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "vector.h" +/* Static math definitions */ + +// Returns an absolute value of n +static long double absld(long double n) { + if (n >= 0.0f) { + return n; + } else { + return -n; + } +} + +// Returns the approximate result of base^exp +static long double powerl(long double base, long double exp); + +static long double n_root(long double a, unsigned int n, unsigned int max_iter, long double epsilon) { + if (n == 1 || a == 0) { + return a; + } + + float prev_val = 1; + float val = 0; + + for (unsigned int i = 0; i < max_iter; i++) { + val = (1.0 / n) * ((n - 1) * prev_val + a / powerl(prev_val, n-1)); + + if (absld(prev_val - val) < epsilon) { + return val; + } + + prev_val = val; + } + + return val; +} + + +// Returns the approximate result of base^exp +static long double powerl(long double base, long double exp) { + // all zeroes|base is zero, but exp is not + if ((base == 0.0f && exp == 0.0f) || (base == 0.0f && exp != 0.0f)) { + return 0.0f; + } + + // exp is 0 + if (exp == 0.0f) { + return 1.0f; + } + + // exp is negative + if (exp < 0.0f) { + return 1.0 / powerl(base, -exp); + } + + // exp is in (0.0; 1.0) + if (exp > 0.0f && exp < 1.0f) { + return n_root(base, 1.0 / exp, 500, 0.0000000001f); + } + + // exp is even + if ((int) exp % 2 == 0) { + float hpow = powerl(base, exp / 2.0f); + return hpow * hpow; + } + + // exp is integer + return base * powerl(base, exp-1.0); +} + +// Returns the approximate result of square root of n +static float sqrootl(long double n) { + return powerl(n, 0.5); +} + +/* End of math definitions */ + +typedef struct vec2 { + long long int x; + long long int y; +} vec2; + +typedef struct vec2f { + long double x; + long double y; +} vec2f; + +// Calculate vector's length +long double vec2_len(vec2 vec); + +// Calculate vector's length +long double vec2f_len(vec2f vec); + +// Multiply 2 vectors the scalar way +long long int vec2_multiply_scalar(vec2 a, vec2 b); + +// Multiply 2 vectors the scalar way +long double vec2f_multiply_scalar(vec2f a, vec2f b); + +// Add 2 vectors together +vec2 vec2_add(vec2 a, vec2 b); + +// Add 2 vectors together +vec2f vec2f_add(vec2f a, vec2f b); + +// Get cosinus of angle between 2 given vectors +long double vec2_angle(vec2 a, vec2 b); + +// Get cosinus of angle between 2 given vectors +long double vec2f_angle(vec2f a, vec2f b); + +typedef struct vec3 { + long long int x; + long long int y; + long long int z; +} vec3; + +typedef struct vec3f { + long double x; + long double y; + long double z; +} vec3f; + +// Calculate vector's length +long double vec3_len(vec3 vec); + +// Calculate vector's length +long double vec3f_len(vec3f vec); + +// Add 2 vectors together +vec3 vec3_add(vec3 a, vec3 b); + +// Add 2 vectors together +vec3f vec3f_add(vec3f a, vec3f b); + +// Multiply 2 vectors the scalar way +long long int vec3_multiply_scalar(vec3 a, vec3 b); + +// Multiply 2 vectors the scalar way +long double vec3f_multiply_scalar(vec3f a, vec3f b); + +// Get cosinus of angle between 2 given vectors +long double vec3_angle(vec3 a, vec3 b); + +// Get cosinus of angle between 2 given vectors +long double vec3f_angle(vec3f a, vec3f b); + +// Get resulting vector of vector multiplication of 2 given vectors +vec3 vec3_multiply_vector(vec3 a, vec3 b); + +// Get resulting vector of vector multiplication of 2 given vectors +vec3f vec3f_multiply_vector(vec3f a, vec3f b); // Calculate vector's length long double vec2_len(vec2 vec) { diff --git a/src/math/vector.h b/src/math/vector.h index ef2411b..017d480 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -1,7 +1,7 @@ /* The MIT License (MIT) -Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) +Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -10,8 +10,6 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "misc.h" - typedef struct vec2 { long long int x; long long int y; diff --git a/src/rng/rng.c b/src/rng/rng.c index 920382e..be94366 100644 --- a/src/rng/rng.c +++ b/src/rng/rng.c @@ -10,7 +10,7 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "rng.h" +#include // Implementation of BBS RNG with pre-defined variables int64_t bbs(int64_t seed) { diff --git a/src/strings/levenshtein.c b/src/strings/levenshtein.c index c37edae..5981997 100644 --- a/src/strings/levenshtein.c +++ b/src/strings/levenshtein.c @@ -1,4 +1,16 @@ -#include "levenshtein.h" +/* +The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include int _min(int a, int b) { if (a < b) { diff --git a/src/strings/levenshtein.h b/src/strings/levenshtein.h index 9f8d942..2e4ecb7 100644 --- a/src/strings/levenshtein.h +++ b/src/strings/levenshtein.h @@ -1,4 +1,14 @@ -#include +/* +The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ // Calculates Levenshtein distance for str1 and str2 int levenshtein_distance(const char* str1, const char* str2); \ No newline at end of file diff --git a/testing/test.c b/testing/test.c index 3793378..be7dfa4 100644 --- a/testing/test.c +++ b/testing/test.c @@ -44,7 +44,7 @@ int test_ppm() { return EXIT_FAILURE; } - if (write_ppm(ppm, "result_image.ppm") != 0) { + if (!write_ppm(ppm, "result_image.ppm")) { printf("[ERROR] failed to write new ppm image\n"); return EXIT_FAILURE; } @@ -59,7 +59,7 @@ int test_ppm() { } } - if (write_ppm(new_ppm, "result_image.ppm") != 0) { + if (!write_ppm(new_ppm, "result_image.ppm")) { printf("[ERROR] failed to write new changed ppm image\n"); return EXIT_FAILURE; } @@ -130,12 +130,23 @@ int test_fs() { printf("[ERROR] Failed to determine file size of test ppm image\n"); return EXIT_FAILURE; } + uint64_t fsize_stat = file_size("test_img512x512.ppm"); + uint64_t fsize_brute = file_size_brute("test_img512x512.ppm"); + if (fsize_brute != fsize_stat) { + printf("[ERROR] File size functions returned different sizes on the same file: (brute - stat) (%ld - %ld)\n", fsize_brute, fsize_stat); + return EXIT_FAILURE; + } if (copy_file("test_img512x512.ppm", "copied_ppm_file.ppm") == -1) { printf("[ERROR] Failed to copy test ppm image\n"); return EXIT_FAILURE; } + if (!can_open_file("test_img512x512.ppm")) { + printf("[ERROR] Failed to open test_img512x512.ppm, which must exist\n"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; }