Browse Source

Added bits package; Removed platform-specific EXIT codes from fs

master
parent
commit
a9b783c305
  1. 44
      src/bits/bits.c
  2. 25
      src/bits/bits.h
  3. 8
      src/fs/fs.c
  4. 2
      src/fs/fs.h
  5. 54
      testing/test.c

44
src/bits/bits.c

@ -0,0 +1,44 @@
/*
The MIT License (MIT)
Copyright © 2022 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 "bits.h"
// 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"
int8_t get_byte_bit(uint8_t byte, uint8_t position) {
if (position > 8 || position < 1) {
return -1;
}
return ((1 << (position-1)) & byte);
}
// Sets position bit in byte to 1. Does nothing if position is not in range [1..8] or if the bit is already 1.
// Position is counted from the least significant bit, ie: in 10010010 position 1 is "0", but position 8 is "1"
void set_byte_bit(uint8_t* byte, uint8_t position) {
if (position > 8 || position < 1) {
return;
}
*byte = (*byte | ((uint8_t) (1 << (position-1))));
}
// Sets position bit in byte to 0. Does nothing if position is not in range [1..8] or if the bit is already 0.
// Position is counted from the least significant bit, ie: in 10010010 position 1 is "0", but position 8 is "1"
void unset_byte_bit(uint8_t* byte, uint8_t position) {
if (position > 8 || position < 1) {
return;
}
*byte = (*byte & ~(1 << (position-1)));
}

25
src/bits/bits.h

@ -0,0 +1,25 @@
/*
The MIT License (MIT)
Copyright © 2022 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 <stdint.h>
// 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"
int8_t get_byte_bit(uint8_t byte, uint8_t position);
// Sets position bit in byte to 1. Does nothing if position is not in range [1..8] or if the bit is already 1.
// Position is counted from the least significant bit, ie: in 10010010 position 1 is "0", but position 8 is "1"
void set_byte_bit(uint8_t* byte, uint8_t position);
// Sets position bit in byte to 0. Does nothing if position is not in range [1..8] or if the bit is already 0.
// Position is counted from the least significant bit, ie: in 10010010 position 1 is "0", but position 8 is "1"
void unset_byte_bit(uint8_t* byte, uint8_t position);

8
src/fs/fs.c

@ -31,7 +31,7 @@ uint64_t file_size(char* path) {
return file_size; return file_size;
} }
// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns EXIT_FAILURE in case of an error // 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) { int copy_file(char* path_src, char* path_dst) {
FILE* src_file; FILE* src_file;
FILE* dst_file; FILE* dst_file;
@ -41,12 +41,12 @@ int copy_file(char* path_src, char* path_dst) {
src_file = fopen(path_src, "rb"); src_file = fopen(path_src, "rb");
if (!src_file) { if (!src_file) {
return EXIT_FAILURE; return -1;
} }
dst_file = fopen(path_dst, "wb"); dst_file = fopen(path_dst, "wb");
if (!dst_file) { if (!dst_file) {
return EXIT_FAILURE; return -1;
} }
while (!feof(src_file)) { while (!feof(src_file)) {
@ -57,5 +57,5 @@ int copy_file(char* path_src, char* path_dst) {
fclose(src_file); fclose(src_file);
fclose(dst_file); fclose(dst_file);
return EXIT_SUCCESS; return 0;
} }

2
src/fs/fs.h

@ -17,5 +17,5 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
// Get file size. returns 18446744073709551615 (uint64_t maximum value) if something went wrong // Get file size. returns 18446744073709551615 (uint64_t maximum value) if something went wrong
uint64_t file_size(char* path); uint64_t file_size(char* path);
// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns EXIT_FAILURE in case of an error // 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); int copy_file(char* path_src, char* path_dst);

54
testing/test.c

@ -21,6 +21,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include "../src/img/ppm.h" #include "../src/img/ppm.h"
#include "../src/endian/endian.h" #include "../src/endian/endian.h"
#include "../src/fs/fs.h" #include "../src/fs/fs.h"
#include "../src/bits/bits.h"
int test_rng() { int test_rng() {
lcg(76); lcg(76);
@ -84,7 +85,7 @@ int test_endian() {
const uint16_t test_num16_swapped = 43105; const uint16_t test_num16_swapped = 43105;
if (swap_endian16(test_num16) != test_num16_swapped) { if (swap_endian16(test_num16) != test_num16_swapped) {
printf("[INFO] Failed to swap endianness for 16bit integer: %u -> %u; supposed to get %u\n", printf("[ERROR] Failed to swap endianness for 16bit integer: %u -> %u; supposed to get %u\n",
test_num16, swap_endian16(test_num16), test_num16_swapped test_num16, swap_endian16(test_num16), test_num16_swapped
); );
return EXIT_FAILURE; return EXIT_FAILURE;
@ -96,7 +97,7 @@ int test_endian() {
const uint32_t test_num32_swapped = 4126043648; const uint32_t test_num32_swapped = 4126043648;
if ((swap_endian32(test_num32)) != test_num32_swapped) { if ((swap_endian32(test_num32)) != test_num32_swapped) {
printf("[INFO] Failed to swap endianness for 32bit integer: %u -> %u; supposed to get %u\n", printf("[ERROR] Failed to swap endianness for 32bit integer: %u -> %u; supposed to get %u\n",
test_num32, swap_endian32(test_num32), test_num32_swapped test_num32, swap_endian32(test_num32), test_num32_swapped
); );
return EXIT_FAILURE; return EXIT_FAILURE;
@ -109,7 +110,7 @@ int test_endian() {
const uint64_t test_num64_swapped = 157878716858368; const uint64_t test_num64_swapped = 157878716858368;
if (swap_endian64(test_num64) != test_num64_swapped) { if (swap_endian64(test_num64) != test_num64_swapped) {
printf("[INFO] Failed to swap endianness for 64bit integer: %lu -> %lu; supposed to get %lu\n", printf("[ERROR] Failed to swap endianness for 64bit integer: %lu -> %lu; supposed to get %lu\n",
test_num64, swap_endian64(test_num64), test_num64_swapped test_num64, swap_endian64(test_num64), test_num64_swapped
); );
return EXIT_FAILURE; return EXIT_FAILURE;
@ -120,12 +121,45 @@ int test_endian() {
int test_fs() { int test_fs() {
if (file_size("test_img512x512.ppm") == UINT64_MAX) { if (file_size("test_img512x512.ppm") == UINT64_MAX) {
printf("[INFO] Failed to determine file size of test ppm image"); printf("[ERROR] Failed to determine file size of test ppm image");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (copy_file("test_img512x512.ppm", "copied_ppm_file.ppm") == EXIT_FAILURE) { if (copy_file("test_img512x512.ppm", "copied_ppm_file.ppm") == -1) {
printf("[INFO] Failed to copy test ppm image\n"); printf("[ERROR] Failed to copy test ppm image\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int test_bits() {
// 11001000
uint8_t test_byte = 200;
// 11001001
const uint8_t test_byte_first_bit_set = 201;
// 11001000
const uint8_t test_byte_first_bit_unset = 200;
if (get_byte_bit(test_byte, 1) != 0) {
printf(
"[ERROR] Failed to correctly determine the first bit in %d: supposed to get %d but got %d instead\n",
test_byte,
get_byte_bit(test_byte, 1),
0);
return EXIT_FAILURE;
}
set_byte_bit(&test_byte, 1);
if (test_byte != test_byte_first_bit_set) {
printf("[ERROR] Failed to set the first bit to 1 in %d\n", test_byte);
return EXIT_FAILURE;
}
unset_byte_bit(&test_byte, 1);
if (test_byte != test_byte_first_bit_unset) {
printf("[ERROR] Failed to unset the first bit in %d\n", test_byte);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -165,5 +199,13 @@ int main() {
printf("[INFO] FS test passed\n\n"); printf("[INFO] FS test passed\n\n");
} }
// bits
printf("[INFO] Testing bits...\n");
if (test_bits() == EXIT_FAILURE) {
printf("[INFO] Bits test failed\n\n");
} else {
printf("[INFO] Bits test passed\n\n");
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
Loading…
Cancel
Save