Browse Source

Auxistr and tests

master
parent
commit
3b9ef3cacd
  1. 4
      src/rng/rng.c
  2. 62
      src/strings/auxistr.c
  3. 20
      src/strings/auxistr.h
  4. 40
      testing/test.c

4
src/rng/rng.c

@ -12,12 +12,12 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include <stdint.h> #include <stdint.h>
// Implementation of BBS RNG with pre-defined variables // Implementation of BBS RNG with pre-defined weights
int64_t bbs(int64_t seed) { int64_t bbs(int64_t seed) {
return seed * seed % (2503 * 3571); return seed * seed % (2503 * 3571);
} }
// Implementation of LCG RNG with pre-defined variables // Implementation of LCG RNG with pre-defined weights
int64_t lcg(int64_t seed) { int64_t lcg(int64_t seed) {
return (8121 * seed + 28411) % 134456; return (8121 * seed + 28411) % 134456;
} }

62
src/strings/auxistr.c

@ -0,0 +1,62 @@
/*
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 <stdlib.h>
// Returns string length not counting '\0'
unsigned long strlength(const char* str) {
unsigned long length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
// Concatenate 2 strings together and return allocated resulting one
char* strconcat(const char* first, const char* second) {
char* resulting_str = NULL;
unsigned long first_length = strlength(first);
unsigned long second_length = strlength(second);
unsigned long total_length = first_length + second_length;
resulting_str = (char*) malloc(total_length + 1);
for (unsigned long i = 0; i < first_length; i++) {
resulting_str[i] = first[i];
}
for (unsigned long i = 0; i < second_length; i++) {
resulting_str[i + first_length] = second[i];
}
resulting_str[total_length] = '\0';
return resulting_str;
}
// Check if 2 strings are equal. Returns 1 if it is true, 0 - otherwise
int streq(const char* first, const char* second) {
unsigned long first_len = strlength(first);
unsigned long second_len = strlength(second);
if (first_len != second_len) {
return 0;
}
for (unsigned long i = 0; i < first_len; i++) {
if (first[i] != second[i]) {
return 0;
}
}
return 1;
}

20
src/strings/auxistr.h

@ -0,0 +1,20 @@
/*
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.
*/
// Returns string length not counting '\0'
unsigned long strlength(const char* str);
// Concatenate 2 strings together and return allocated resulting one
char* strconcat(const char* first, const char* second);
// Check if 2 strings are equal. Returns 1 if it is true, 0 - otherwise
int streq(const char* first, const char* second);

40
testing/test.c

@ -28,6 +28,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include "../src/math/numerics.h" #include "../src/math/numerics.h"
#include "../src/datastruct/cvec.h" #include "../src/datastruct/cvec.h"
#include "../src/strings/levenshtein.h" #include "../src/strings/levenshtein.h"
#include "../src/strings/auxistr.h"
#include "../src/crypt/xorcipher.h" #include "../src/crypt/xorcipher.h"
int test_rng() { int test_rng() {
@ -373,12 +374,51 @@ int test_levenshtein() {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int test_auxistr() {
const char* ascii_text = "There are 38 characters in this string";
if (strlength(ascii_text) != 38) {
printf("[ERROR] A text with 38 characters was determined to be of length %ld\n", strlength(ascii_text));
return EXIT_FAILURE;
}
const char* other_ascii_text = "..-.";
if (streq(ascii_text, other_ascii_text)) {
printf("[ERROR] \"%s\" was determined to be equal to \"%s\"\n", ascii_text, other_ascii_text);
return EXIT_FAILURE;
}
const char* first = "日本語";
const char* second = "текст";
const char* second_copy = "текст";
if (!streq(second, second_copy)) {
printf("[ERROR] \"%s\" was determined to be different from \"%s\"\n", second, second_copy);
return EXIT_FAILURE;
}
char* concatenated = strconcat(first, second);
if (!streq(concatenated, "日本語текст")) {
printf("[ERROR] \"%s\" was not concatenated correctly into \"%s\"\n", concatenated, "日本語текст");
free(concatenated);
return EXIT_FAILURE;
}
free(concatenated);
return EXIT_SUCCESS;
}
int test_strings() { int test_strings() {
if (test_levenshtein() == EXIT_FAILURE) { if (test_levenshtein() == EXIT_FAILURE) {
printf("[ERROR] Levenshtein test failed\n"); printf("[ERROR] Levenshtein test failed\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (test_auxistr() == EXIT_FAILURE) {
printf("[ERROR] Auxistr test failed\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

Loading…
Cancel
Save