6033: Add more tests for hashing, `CSemaphore` and utf8 confusables r=heinrich5991 a=Robyt3



## Checklist

- [ ] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [X] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2022-11-12 20:28:52 +00:00 committed by GitHub
commit bc4a5ee6f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 9 deletions

View file

@ -100,7 +100,7 @@ void InitAndroid()
sha256_from_str(&ShaAllFile, vLines[0].c_str());
// TODO: check files individually
if(!GotSHA || sha256_comp(ShaAllFile, ShaAll) != 0)
if(!GotSHA || ShaAllFile != ShaAll)
{
// then the files
for(size_t i = 1; i < vLines.size(); ++i)

View file

@ -2048,9 +2048,6 @@ char str_uppercase(char c);
int str_isallnum(const char *str);
unsigned str_quickhash(const char *str);
struct SKELETON;
void str_utf8_skeleton_begin(struct SKELETON *skel, const char *str);
int str_utf8_skeleton_next(struct SKELETON *skel);
int str_utf8_to_skeleton(const char *str, int *buf, int buf_len);
/*

View file

@ -1,6 +1,6 @@
#include "confusables.h"
#include "../system.h"
#include <base/system.h>
#include <cstddef>
@ -35,14 +35,14 @@ struct SKELETON
const char *str;
};
void str_utf8_skeleton_begin(struct SKELETON *skel, const char *str)
static void str_utf8_skeleton_begin(struct SKELETON *skel, const char *str)
{
skel->skeleton = NULL;
skel->skeleton_len = 0;
skel->str = str;
}
int str_utf8_skeleton_next(struct SKELETON *skel)
static int str_utf8_skeleton_next(struct SKELETON *skel)
{
int ch = 0;
while(skel->skeleton_len == 0)

View file

@ -52,7 +52,10 @@ TEST(Hash, Sha256ToStringLargeBuffer)
TEST(Hash, Sha256Eq)
{
EXPECT_EQ(sha256("", 0), sha256("", 0));
EXPECT_EQ(sha256_comp(sha256("", 0), sha256("", 0)), 0);
EXPECT_TRUE(sha256("", 0) == sha256("", 0));
EXPECT_NE(sha256_comp(sha256("a", 1), sha256("b", 1)), 0);
EXPECT_TRUE(sha256("a", 1) != sha256("b", 1));
}
TEST(Hash, Sha256FromStr)
@ -149,7 +152,10 @@ TEST(Hash, Md5ToStringLargeBuffer)
TEST(Hash, Md5Eq)
{
EXPECT_EQ(md5("", 0), md5("", 0));
EXPECT_EQ(md5_comp(md5("", 0), md5("", 0)), 0);
EXPECT_TRUE(md5("", 0) == md5("", 0));
EXPECT_NE(md5_comp(md5("a", 1), md5("b", 1)), 0);
EXPECT_TRUE(md5("a", 1) != md5("b", 1));
}
TEST(Hash, Md5FromStr)

View file

@ -58,12 +58,49 @@ TEST(Str, Utf8CompConfusables)
{
EXPECT_TRUE(str_utf8_comp_confusable("abc", "abc") == 0);
EXPECT_TRUE(str_utf8_comp_confusable("rn", "m") == 0);
EXPECT_TRUE(str_utf8_comp_confusable("m", "rn") == 0);
EXPECT_TRUE(str_utf8_comp_confusable("rna", "ma") == 0);
EXPECT_TRUE(str_utf8_comp_confusable("ma", "rna") == 0);
EXPECT_FALSE(str_utf8_comp_confusable("mA", "rna") == 0);
EXPECT_FALSE(str_utf8_comp_confusable("ma", "rnA") == 0);
EXPECT_TRUE(str_utf8_comp_confusable("arn", "am") == 0);
EXPECT_TRUE(str_utf8_comp_confusable("am", "arn") == 0);
EXPECT_FALSE(str_utf8_comp_confusable("Am", "arn") == 0);
EXPECT_FALSE(str_utf8_comp_confusable("am", "Arn") == 0);
EXPECT_TRUE(str_utf8_comp_confusable("l", "ӏ") == 0); // CYRILLIC SMALL LETTER PALOCHKA
EXPECT_TRUE(str_utf8_comp_confusable("i", "¡") == 0); // INVERTED EXCLAMATION MARK
EXPECT_FALSE(str_utf8_comp_confusable("o", "x") == 0);
EXPECT_TRUE(str_utf8_comp_confusable("aceiou", "ąçęįǫų") == 0);
}
TEST(Str, Utf8ToSkeleton)
{
int aBuf[32];
EXPECT_EQ(str_utf8_to_skeleton("abc", aBuf, 0), 0);
EXPECT_EQ(str_utf8_to_skeleton("", aBuf, std::size(aBuf)), 0);
EXPECT_EQ(str_utf8_to_skeleton("abc", aBuf, std::size(aBuf)), 3);
EXPECT_EQ(aBuf[0], 'a');
EXPECT_EQ(aBuf[1], 'b');
EXPECT_EQ(aBuf[2], 'c');
EXPECT_EQ(str_utf8_to_skeleton("m", aBuf, std::size(aBuf)), 2);
EXPECT_EQ(aBuf[0], 'r');
EXPECT_EQ(aBuf[1], 'n');
EXPECT_EQ(str_utf8_to_skeleton("rn", aBuf, std::size(aBuf)), 2);
EXPECT_EQ(aBuf[0], 'r');
EXPECT_EQ(aBuf[1], 'n');
EXPECT_EQ(str_utf8_to_skeleton("ӏ", aBuf, std::size(aBuf)), 1); // CYRILLIC SMALL LETTER PALOCHKA
EXPECT_EQ(aBuf[0], 'i');
EXPECT_EQ(str_utf8_to_skeleton("¡", aBuf, std::size(aBuf)), 1); // INVERTED EXCLAMATION MARK
EXPECT_EQ(aBuf[0], 'i');
EXPECT_EQ(str_utf8_to_skeleton("ąçęįǫų", aBuf, std::size(aBuf)), 6);
EXPECT_EQ(aBuf[0], 'a');
EXPECT_EQ(aBuf[1], 'c');
EXPECT_EQ(aBuf[2], 'e');
EXPECT_EQ(aBuf[3], 'i');
EXPECT_EQ(aBuf[4], 'o');
EXPECT_EQ(aBuf[5], 'u');
}
TEST(Str, Utf8ToLower)
{
EXPECT_TRUE(str_utf8_tolower('A') == 'a');

View file

@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include <base/system.h>
#include <base/tl/threading.h>
static void Nothing(void *pUser)
{
@ -49,6 +50,20 @@ TEST(Thread, SemaphoreSingleThreaded)
sphore_destroy(&Semaphore);
}
TEST(Thread, SemaphoreWrapperSingleThreaded)
{
CSemaphore Semaphore;
EXPECT_EQ(Semaphore.GetApproximateValue(), 0);
Semaphore.Signal();
EXPECT_EQ(Semaphore.GetApproximateValue(), 1);
Semaphore.Signal();
EXPECT_EQ(Semaphore.GetApproximateValue(), 2);
Semaphore.Wait();
EXPECT_EQ(Semaphore.GetApproximateValue(), 1);
Semaphore.Wait();
EXPECT_EQ(Semaphore.GetApproximateValue(), 0);
}
static void SemaphoreThread(void *pUser)
{
SEMAPHORE *pSemaphore = (SEMAPHORE *)pUser;