fopen and tests

This commit is contained in:
nora 2023-10-04 21:35:18 +02:00
parent 862ef8dc22
commit b795ee80c9
14 changed files with 97 additions and 45 deletions

View file

@ -1,9 +1,10 @@
#include <stdio.h>
#include <assert.h>
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
char *self = argv[0];
char first = self[0];
if (first != '/') {
return 1;
}
assert((first < 128) && "argv[0] is not ascii/utf-8");
}

16
tests/c/fopen.c Normal file
View file

@ -0,0 +1,16 @@
#include <stdio.h>
#include <errno.h>
#include <assert.h>
int main(int argc, char *argv[]) {
FILE *file1 = fopen("./c/fopen.c", "r");
assert(file1 && "failed to open file");
FILE *file2 = fopen("./c/fopen.c", "meow");
assert(!file2 && "succeeded opening file despite invalid argument");
assert((errno == EINVAL) && "wrong errno");
FILE *file3 = fopen("/this-does-absolutely-not-exist-at-all-edhjkefhew98", "r");
assert(!file3 && "succeeded despite file not existing");
assert((errno == ENOENT) && "wrong errno");
}

View file

@ -1,14 +1,12 @@
#include <stdlib.h>
#include <assert.h>
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
char *env = getenv("PATH");
if (!env) {
return 1;
}
assert(env && "PATH doesnt exist");
char *env2 = getenv(
"__some absolutely NONSENSE that no one would ever define please..");
if (env2) {
return 1;
}
assert(!env2 && "nonsense environment variable found");
}

View file

@ -1,7 +1,10 @@
#include <stdlib.h>
#include <assert.h>
int main(void) {
int main(void)
{
char *alloc = (char *)malloc(10);
assert(alloc && "allocation failed");
*alloc = 1;
*(alloc + 9) = 2;
free(alloc);

View file

@ -1,13 +1,14 @@
#include <string.h>
#include <assert.h>
int main(void) {
int main(void)
{
char buf[10];
memset(buf, 34, sizeof(buf));
for (int i = 0; i < 10; ++i) {
if (buf[i] != 34) {
return 1;
}
for (int i = 0; i < 10; ++i)
{
assert((buf[i] == 34) && "memset failed to write byte");
}
}

View file

@ -1,8 +1,8 @@
#include <stdio.h>
#include <assert.h>
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
int result = printf("Hello, world!\n");
if (result != 14) {
return 1;
}
assert((result == 14) && "printf returned wrong number of chars");
}

View file

@ -1,9 +1,9 @@
#include <stdlib.h>
#include <assert.h>
int main(void) {
int main(void)
{
char *str = "12";
long value = strtol(str, NULL, 10);
if (value != 12) {
return 1;
}
assert((value == 12) && "value must be 12");
}