<commit_before>#include "types.h"
#include "user.h"
#include "fs.h"
#include "traps.h"
#include "pthread.h"
#include "rnd.hh"

#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#include <sys/wait.h>

#include <utility>

char buf[2048];
char name[3];
const char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 };

// simple file system tests

void
opentest(void)
{
  int fd;

  fprintf(stdout, "open test\n");
  fd = open("echo", 0);
  if(fd < 0)
    die("open echo failed!");
  close(fd);
  fd = open("doesnotexist", 0);
  if(fd >= 0)
    die("open doesnotexist succeeded!");
  fprintf(stdout, "open test ok\n");
}

void
writetest(void)
{
  int fd;
  int i;

  fprintf(stdout, "small file test\n");
  fd = open("small", O_CREAT|O_RDWR, 0666);
  if(fd >= 0){
    fprintf(stdout, "creat small succeeded; ok\n");
  } else {
    die("error: creat small failed!");
  }
  for(i = 0; i < 100; i++){
    if(write(fd, "aaaaaaaaaa", 10) != 10)
      die("error: write aa %d new file failed", i);
    if(write(fd, "bbbbbbbbbb", 10) != 10)
      die("error: write bb %d new file failed", i);
  }
  fprintf(stdout, "writes ok\n");
  close(fd);
  fd = open("small", O_RDONLY);
  if(fd >= 0){
    fprintf(stdout, "open small succeeded ok\n");
  } else {
    die("error: open small failed!");
  }
  i = read(fd, buf, 2000);
  if(i == 2000){
    fprintf(stdout, "read succeeded ok\n");
  } else {
    die("read failed");
  }
  close(fd);

  if(unlink("small") < 0)
    die("unlink small failed");
  fprintf(stdout, "small file test ok\n");
}

void
writetest1(void)
{
  int fd, n;

  fprintf(stdout, "big files test\n");

  fd = open("big", O_CREAT|O_RDWR, 0666);
  if(fd < 0)
    die("error: creat big failed!");

  for(u32 i = 0; i < MAXFILE; i++){
    ((int*)buf)[0] = i;
    if(write(fd, buf, 512) != 512)
      die("error: write big file %d failed", i);
  }

  close(fd);

  fd = open("big", O_RDONLY);
  if(fd < 0)
    die("error: open big failed!");

  n = 0;
  for(;;){
    u32 i = read(fd, buf, 512);
    if(i == 0){
      if(n == MAXFILE - 1)
        die("read only %d blocks from big", n);<commit_msg>usertests: add a helpful check to writetest1<commit_after>#include "types.h"
#include "user.h"
#include "fs.h"
#include "traps.h"
#include "pthread.h"
#include "rnd.hh"

#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#include <sys/wait.h>

#include <utility>

char buf[2048];
char name[3];
const char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 };

// simple file system tests

void
opentest(void)
{
  int fd;

  fprintf(stdout, "open test\n");
  fd = open("echo", 0);
  if(fd < 0)
    die("open echo failed!");
  close(fd);
  fd = open("doesnotexist", 0);
  if(fd >= 0)
    die("open doesnotexist succeeded!");
  fprintf(stdout, "open test ok\n");
}

void
writetest(void)
{
  int fd;
  int i;

  fprintf(stdout, "small file test\n");
  fd = open("small", O_CREAT|O_RDWR, 0666);
  if(fd >= 0){
    fprintf(stdout, "creat small succeeded; ok\n");
  } else {
    die("error: creat small failed!");
  }
  for(i = 0; i < 100; i++){
    if(write(fd, "aaaaaaaaaa", 10) != 10)
      die("error: write aa %d new file failed", i);
    if(write(fd, "bbbbbbbbbb", 10) != 10)
      die("error: write bb %d new file failed", i);
  }
  fprintf(stdout, "writes ok\n");
  close(fd);
  fd = open("small", O_RDONLY);
  if(fd >= 0){
    fprintf(stdout, "open small succeeded ok\n");
  } else {
    die("error: open small failed!");
  }
  i = read(fd, buf, 2000);
  if(i == 2000){
    fprintf(stdout, "read succeeded ok\n");
  } else {
    die("read failed");
  }
  close(fd);

  if(unlink("small") < 0)
    die("unlink small failed");
  fprintf(stdout, "small file test ok\n");
}

void
writetest1(void)
{
  int fd, n;

  fprintf(stdout, "big files test\n");

  fd = open("big", O_CREAT|O_RDWR, 0666);
  if(fd < 0)
    die("error: creat big failed!");

  for(u32 i = 0; i < MAXFILE; i++){
    ((int*)buf)[0] = i;
    if(write(fd, buf, 512) != 512)
      die("error: write big file %d failed", i);
  }

  close(fd);

  fd = open("big", O_RDONLY);
  if(fd < 0)
    die("error: open big failed!");

  struct stat st;
  if (fstat(fd, &st) < 0)
    die("error: stat big failed!");

  if (st.st_size != MAXFILE * 512)
    die("error: wrong stat size: %ld != %ld (%ld * 512)",
        st.st_size, MAXFILE * 512, MAXFILE);

  n = 0;
  for(;;){
    u32 i = read(fd, buf, 512);
    if(i == 0){
      if(n == MAXFILE - 1)
        die("read only %d blocks from big", n);
{'fraction_non_alphanumeric': 0.19927148060852795, 'fraction_numerical': 0.028926505249625027, 'mean_word_length': 3.251366120218579, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2831310', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1868, 'n_words': 516}