class TestCase::Sys {
use Sys;
use Sys::IO;
use Sys::OS;
use Sys::IO::Constant as IO;
use Sys::Ioctl::Constant as IOCTL;
use Sys::Socket::Constant as SOCKET;
use Fn;
use Errno;
our $TEST_DIR : rw string;
static method TEST_DIR_FTEST : string () { return "$TEST_DIR/ftest"; }
static method A : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $atime = Sys->A($file);
warn "[Test Output]Sys->A:$atime";
unless ($atime > 0) {
return 0;
}
}
}
# Excetpions
{
{
my $file = "t/ftest/file_not_exists.txt";
eval { Sys->A($file); }
unless ($@) {
return 0;
}
}
}
return 1;
}
static method C : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ctime = Sys->C($file);
warn "[Test Output]Sys->C:$ctime";
unless ($ctime > 0) {
return 0;
}
}
}
# Excetpions
{
{
my $file = "t/ftest/file_not_exists.txt";
eval { Sys->C($file); }
unless ($@) {
return 0;
}
}
}
return 1;
}
static method M : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $mtime = Sys->M($file);
warn "[Test Output]Sys->M:$mtime";
unless ($mtime > 0) {
return 0;
}
}
}
# Excetpions
{
{
my $file = "t/ftest/file_not_exists.txt";
eval { Sys->M($file); }
unless ($@) {
return 0;
}
}
}
return 1;
}
static method O : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->O($file);
warn("[Test Output]Sys->O:$ok");
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->O($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method R : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->R($file);
unless ($ok == 1) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->R($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method S : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->S($file);
warn "[Test Output]Sys->S:$ok";
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->S($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method W : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->W($file);
unless ($ok == 1) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->W($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method X : int () {
{
{
my $file = "t/ftest/myexe.exe";
my $ok = Sys->X($file);
unless ($ok == 1) {
return 0;
}
}
}
{
{
my $file = "t/ftest/myexe.bat";
my $ok = Sys->X($file);
unless ($ok == 1) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->X($file);
unless ($ok == 0) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->X($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method b : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->b($file);
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->b($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method d : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->d($file);
unless ($ok == 0) {
return 0;
}
}
{
my $file = "t/ftest/dir_empty";
my $ok = Sys->d($file);
unless ($ok == 1) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->d($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
$@ = undef;
return 1;
}
static method e : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->e($file);
unless ($ok == 1) {
return 0;
}
}
{
my $file = "t/ftest/dir_empty";
my $ok = Sys->e($file);
unless ($ok == 1) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->e($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method f : int () {
{
{
my $file = "t/ftest/not_found_file";
my $ok = Sys->f($file);
unless ($ok == 0) {
return 0;
}
}
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->f($file);
unless ($ok == 1) {
return 0;
}
}
{
my $file = "t/ftest/dir_empty";
my $ok = Sys->f($file);
unless ($ok == 0) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->f($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
$@ = undef;
return 1;
}
static method g : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->g($file);
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->g($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method k : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->k($file);
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->k($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method l : int () {
{
# File
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->l($file);
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->l($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method o : int () {
{
# File
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->o($file);
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->o($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method p : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->p($file);
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->p($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method r : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->r($file);
unless ($ok == 1) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->r($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method s : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $size = Sys->s($file);
unless ($size == 0) {
return 0;
}
}
}
# Excetpions
{
{
my $file = "t/ftest/file_not_exists.txt";
eval { Sys->s($file); }
unless ($@) {
return 0;
}
}
}
return 1;
}
static method u : int () {
{
# File
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->u($file);
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->u($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method w : int () {
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->w($file);
unless ($ok == 1) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->w($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method x : int () {
{
{
my $file = "t/ftest/myexe.exe";
my $ok = Sys->x($file);
unless ($ok == 1) {
return 0;
}
}
}
{
{
my $file = "t/ftest/myexe.bat";
my $ok = Sys->x($file);
unless ($ok == 1) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_empty.txt";
my $ok = Sys->x($file);
unless ($ok == 0) {
return 0;
}
}
}
{
{
my $file = "t/ftest/file_not_exists.txt";
Errno->set_errno(0);
my $ok = Sys->x($file);
unless ($ok == 0) {
return 0;
}
unless (Errno->errno > 0) {
return 0;
}
}
}
return 1;
}
static method z : int () {
{
# File
{
my $file = "t/ftest/file_empty.txt";
my $non_zero = Sys->z($file);
unless ($non_zero) {
return 0;
}
}
}
# Excetpions
{
{
my $file = "t/ftest/file_not_exists.txt";
eval { Sys->z($file); }
unless ($@) {
return 0;
}
}
}
return 1;
}
static method readlink : int ($test_dir : string) {
my $sep = (string)undef;
if (Sys::OS->is_windows) {
$sep = "\\";
}
else {
$sep = "/";
}
my $file = "$test_dir${sep}foo.txt";
{
my $stream = Sys::IO->fopen($file, "wb");
unless ($stream) {
return 0;
}
Sys::IO->fclose($stream);
}
my $file_symlink = "$test_dir${sep}foo_link";
{
Sys->symlink($file, $file_symlink);
my $exists_symlink_file = Sys->l($file_symlink);
unless ($exists_symlink_file) {
return 0;
}
}
{
my $buffer = (mutable string)new_string_len 255;
my $ret_file = Sys->readlink($file_symlink);
unless ($ret_file eq $file) {
return 0;
}
}
return 1;
}
static method mkdir : int ($test_dir : string) {
my $dir = "$test_dir/foo";
Sys->mkdir($dir, 0755);
unless (Sys->d($dir)) {
return 0;
}
{
my $dir = "$test_dir/foo_no_mode";
Sys->mkdir($dir);
unless (Sys->d($dir)) {
return 0;
}
}
{
eval { Sys->mkdir($dir, 0755); };
unless (Fn->contains($@, "foo")) {
return 0;
}
}
return 1;
}
static method umask : int ($test_dir : string) {
my $dir = "$test_dir/foo";
my $old_mask = Sys->umask(IO->S_IRUSR);
my $cur_mask = Sys->umask(IO->S_IWUSR);
unless ($cur_mask == IO->S_IRUSR) {
return 0;
}
# Restore
Sys::IO->umask($old_mask);
return 1;
}
static method rmdir : int ($test_dir : string) {
my $dir = "$test_dir/foo";
Sys->mkdir($dir, 0755);
unless (Sys->d($dir)) {
return 0;
}
Sys->rmdir($dir);
if (Sys->e($dir)) {
return 0;
}
{
eval { Sys->rmdir($dir); };
unless (Fn->contains($@, "foo")) {
return 0;
}
}
return 1;
}
static method chdir : int ($test_dir : string) {
my $dir = "$test_dir/ftest/dir_empty";
Sys->chdir($dir);
my $cwd = Sys::IO->getcwd(undef, 0);
unless (Fn->contains($cwd, "dir_empty")) {
return 0;
}
{
eval { Sys->chdir("$dir/not_found.txt"); };
unless (Fn->contains($@, "not_found.txt")) {
return 0;
}
}
return 1;
}
static method chmod : int ($test_dir : string) {
my $file = "$test_dir/foo.txt";
my $stream = Sys::IO->fopen($file, "w");
unless (Sys->f($file)) {
return 0;
}
Sys::IO->fclose($stream);
Sys->chmod(0755, $file);
{
eval { Sys->chmod(0755, "$file/not_found.txt"); };
unless (Fn->contains($@, "not_found.txt")) {
return 0;
}
}
return 1;
}
static method unlink : int ($test_dir : string) {
my $file = "$test_dir/foo.txt";
{
Sys::IO->fopen($file, "w");
}
unless (Sys->f($file)) {
return 0;
}
Sys->unlink($file);
if (Sys->e($file)) {
return 0;
}
{
eval { Sys->unlink($file); };
unless (Fn->contains($@, "foo.txt")) {
return 0;
}
}
return 1;
}
static method rename : int ($test_dir : string) {
my $file = "$test_dir/foo.txt";
{
Sys::IO->fopen($file, "w");
}
unless (Sys->e($file)) {
return 0;
}
my $new_file = "$test_dir/foo_new.txt";
Sys::IO->rename($file, $new_file);
if (Sys->e($file)) {
return 0;
}
unless (Sys->e($new_file)) {
return 0;
}
{
eval { Sys::IO->rename($file, $new_file); };
unless (Fn->contains($@, "foo.txt")) {
return 0;
}
unless (Fn->contains($@, "foo_new.txt")) {
return 0;
}
}
return 1;
}
static method ioctl : int () {
# Socket
my $socket = Sys::Socket->socket(SOCKET->AF_INET, SOCKET->SOCK_STREAM, 0);
unless ($socket> 0) {
return 0;
}
Fn->defer([$socket : int] method :void () {
Sys::Socket->close($socket);
});
my $value_ref = [1];
my $status_ioctl = 0;
eval { $status_ioctl = Sys->ioctl($socket, IOCTL->FIONBIO, $value_ref); };
if (Sys::OS->is_windows) {
unless ($status_ioctl == 0) {
return 0;
}
}
else {
if ($@) {
warn "[Test Output]$@";
}
else {
unless ($status_ioctl == 0) {
return 0;
}
}
}
return 1;
}
static method env : int () {
{
Sys->set_env("NAME", "VALUE");
my $value = Sys->env("NAME");
unless ($value eq "VALUE") {
return 0;
}
}
{
Sys->set_env("NAME", "");
my $value = Sys->env("NAME");
if ($value) {
return 0;
}
}
{
Sys->set_env("NAME", undef);
my $value = Sys->env("NAME");
if ($value) {
return 0;
}
}
# Excetpion
{
eval { Sys->set_env(undef, "VALUE"); }
unless ($@) {
return 0;
}
}
return 1;
}
static method set_env : int () {
return &env;
}
static method opendir : int () {
my $test_dir = &TEST_DIR;
my $test_dir_ftest = &TEST_DIR_FTEST;
{
my $dir_stream_ref = [(Sys::IO::DirStream)undef];
Sys->opendir($dir_stream_ref, $test_dir_ftest);
my $dir_stream = $dir_stream_ref->[0];
unless ($dir_stream) {
return 0;
}
}
return 1;
}
static method readdir : int () {
my $test_dir_ftest = &TEST_DIR_FTEST;
{
my $dir_stream_ref = [(Sys::IO::DirStream)undef];
Sys->opendir($dir_stream_ref, $test_dir_ftest);
my $dir_stream = $dir_stream_ref->[0];
Fn->defer([$dir_stream : Sys::IO::DirStream] method : void () {
Sys->closedir($dir_stream);
});
unless ($dir_stream) {
return 0;
}
Sys->rewinddir($dir_stream);
Sys->rewinddir($dir_stream);
{
my $current_offset = Sys->telldir($dir_stream);
unless ($current_offset == 0) {
return 0;
}
}
{
my $current_offset = Sys->telldir($dir_stream);
unless ($current_offset == 0) {
return 0;
}
}
my $check_file_count = 0;
while (my $dirent = Sys->readdir($dir_stream)) {
my $d_name = $dirent->d_name;
if ($d_name eq "foo.txt") {
$check_file_count++;
}
elsif ($d_name eq "bar.txt") {
$check_file_count++;
}
}
unless ($check_file_count == 2) {
return 0;
}
}
return 1;
}
static method seekdir : int () {
my $test_dir_ftest = &TEST_DIR_FTEST;
{
my $dir_stream_ref = [(Sys::IO::DirStream)undef];
Sys->opendir($dir_stream_ref, $test_dir_ftest);
my $dir_stream = $dir_stream_ref->[0];
my $dirent = Sys->readdir($dir_stream);
my $offset = Sys->telldir($dir_stream);
my $dirent_next = Sys->readdir($dir_stream);
my $dirent_next_d_name = $dirent_next->d_name;
Sys->seekdir($dir_stream, $offset);
my $dirent2 = Sys->readdir($dir_stream);
my $dirent2_d_name = $dirent2->d_name;
unless ($dirent) {
warn;
return 0;
}
unless ($dirent2) {
warn;
return 0;
}
unless ($dirent2_d_name eq $dirent_next_d_name) {
warn;
return 0;
}
}
return 1;
}
static method telldir : int () {
my $test_dir_ftest = &TEST_DIR_FTEST;
{
my $dir_stream_ref = [(Sys::IO::DirStream)undef];
Sys->opendir($dir_stream_ref, $test_dir_ftest);
my $dir_stream = $dir_stream_ref->[0];
my $dirent = Sys->readdir($dir_stream);
my $offset = Sys->telldir($dir_stream);
my $dirent_next = Sys->readdir($dir_stream);
my $dirent_next_d_name = $dirent_next->d_name;
Sys->seekdir($dir_stream, $offset);
my $dirent2 = Sys->readdir($dir_stream);
my $dirent2_d_name = $dirent2->d_name;
unless ($dirent) {
warn;
return 0;
}
unless ($dirent2) {
warn;
return 0;
}
unless ($dirent2_d_name eq $dirent_next_d_name) {
warn;
return 0;
}
}
return 1;
}
static method rewinddir : int () {
my $test_dir_ftest = &TEST_DIR_FTEST;
{
my $dir_stream_ref = [(Sys::IO::DirStream)undef];
Sys->opendir($dir_stream_ref, $test_dir_ftest);
my $dir_stream = $dir_stream_ref->[0];
my $dirent = Sys->readdir($dir_stream);
Sys->rewinddir($dir_stream);
my $dirent2 = Sys->readdir($dir_stream);
unless ($dirent) {
warn;
return 0;
}
unless ($dirent2) {
warn;
return 0;
}
unless ($dirent2->d_name eq $dirent->d_name) {
warn;
return 0;
}
}
return 1;
}
static method closedir : int () {
my $test_dir_ftest = &TEST_DIR_FTEST;
{
my $dir_stream_ref = [(Sys::IO::DirStream)undef];
Sys->opendir($dir_stream_ref, $test_dir_ftest);
my $dir_stream = $dir_stream_ref->[0];
unless ($dir_stream) {
return 0;
}
if ($dir_stream->closed) {
warn $dir_stream->closed;
return 0;
}
Sys->closedir($dir_stream);
unless ($dir_stream->closed) {
warn $dir_stream->closed;
return 0;
}
}
$@ = undef;
return 1;
}
# rand (srand is called from rand)
static method rand : int () {
# 0 <= random_number < 1
{
my $rand = 0.0;
my $invalid_range = 0;
for (my $i = 0; $i < 100000; $i++) {
$rand = Sys->rand;
unless ($rand >= 0 && $rand < 1) {
$invalid_range = 1;
last;
}
}
if ($invalid_range) {
return 0;
}
}
# Apper random numbers
{
my $founds_plus = new int[10];
my $founds_minus = new int[10];
my $rand = 0.0;
for (my $i = 0; $i < 100000; $i++) {
$rand = Sys->rand;
# 0-9
my $dig0 = (int)($rand * 10);
$founds_plus->[$dig0] = 1;
}
unless (Array->equals_int($founds_plus, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1])) {
return 0;
}
}
# Apper random numbers
{
my $founds_plus = new int[10];
my $founds_minus = new int[10];
my $rand = 0.0;
for (my $i = 0; $i < 100000; $i++) {
$rand = Sys->rand(10);
# 0-9
my $dig0 = (int)($rand);
$founds_plus->[$dig0] = 1;
}
unless (Array->equals_int($founds_plus, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1])) {
return 0;
}
}
return 1;
}
}