Compare commits

...

2 Commits

Author SHA1 Message Date
1f5f6a5e3b libc: Add strcspn (with a test) 2022-10-22 11:57:25 +02:00
6816a5b11f Scheduler: do not reboot on PID 1 exit if we are in a test 2022-10-22 11:56:08 +02:00
5 changed files with 48 additions and 1 deletions

View File

@ -279,7 +279,14 @@ void sched_common_exit(Context* context, int64_t status)
return true; return true;
}); });
} }
else { reboot(); } else
{
#ifndef RUN_TEST_AS_INIT
reboot();
#else
hang();
#endif
}
Scheduler::task_yield(context); Scheduler::task_yield(context);
} }

View File

@ -48,6 +48,9 @@ extern "C"
/* Concatenates at most max bytes of the string src into dest. */ /* Concatenates at most max bytes of the string src into dest. */
char* strncat(char* dest, const char* src, size_t max); char* strncat(char* dest, const char* src, size_t max);
/* Returns the length of the initial segment of str which consists entirely of bytes not in reject. */
size_t strcspn(const char* str, const char* reject);
/* Compares strings a and b. You might prefer to use the safer strncmp function. */ /* Compares strings a and b. You might prefer to use the safer strncmp function. */
int strcmp(const char* a, const char* b); int strcmp(const char* a, const char* b);

View File

@ -119,6 +119,22 @@ extern "C"
return *(const unsigned char*)a - *(const unsigned char*)b; return *(const unsigned char*)a - *(const unsigned char*)b;
} }
size_t strcspn(const char* str, const char* reject)
{
const char* s = str;
while (*s)
{
const char* rp = reject;
while (*rp)
{
if (*s == *rp) return s - str;
rp++;
}
s++;
}
return s - str;
}
char* strcat(char* dest, const char* src) char* strcat(char* dest, const char* src)
{ {
size_t dest_len = strlen(dest); size_t dest_len = strlen(dest);

View File

@ -2,10 +2,12 @@
DEFINE_TEST(strlen); DEFINE_TEST(strlen);
DEFINE_TEST(strnlen); DEFINE_TEST(strnlen);
DEFINE_TEST(strcspn);
int main() int main()
{ {
START_TEST_CASE(string.h); START_TEST_CASE(string.h);
RUN_TEST(strlen); RUN_TEST(strlen);
RUN_TEST(strnlen); RUN_TEST(strnlen);
RUN_TEST(strcspn);
} }

View File

@ -40,5 +40,24 @@ DEFINE_TEST(strnlen)
EXPECT_EQ(len, sizeof(buf)); EXPECT_EQ(len, sizeof(buf));
TEST_SUCCESS();
}
DEFINE_TEST(strcspn)
{
START_TEST(strcspn);
const char* str = "This string has vowels";
const char* vowels = "aeiou";
size_t len = strcspn(str, vowels);
EXPECT_EQ(len, 2);
str = "WWWWW";
len = strcspn(str, vowels);
EXPECT_EQ(len, 5);
TEST_SUCCESS(); TEST_SUCCESS();
} }