1169d92004-05-02Martin Nilsson START_MARKER
457cf02002-11-26Martin Nilsson dnl tests for file module
ab6aec1997-02-11Fredrik Hübinette (Hubbe) test_true(programp(Stdio.File)) test_true(programp(Stdio.File))
05c7cd1997-07-19Fredrik Hübinette (Hubbe) test_true(objectp(Stdio.File()))
ab6aec1997-02-11Fredrik Hübinette (Hubbe) test_true(programp(Stdio.Port))
05c7cd1997-07-19Fredrik Hübinette (Hubbe) test_true(objectp(Stdio.Port()))
457cf02002-11-26Martin Nilsson test_any(object o; o=Stdio.File(); destruct(o); return 1,1)
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
32cc772004-03-21Henrik Grubbström (Grubba) dnl - mkdir test_any([[ Stdio.Stat st = file_stat("/tmp"); string prefix = (st && st->isdir)?"/tmp/":""; int pid = getpid(); int i; prefix += "conftest_pike_dir_"; for(i = 0; i < 32; i++) { string testdir = prefix + i;
601fdb2015-05-30Henrik Grubbström (Grubba)  rm(testdir); if (mkdir(testdir) || rm(testdir + "/conftest")) {
32cc772004-03-21Henrik Grubbström (Grubba)  add_constant("testdir", testdir + "/"); add_constant("testfile", testdir + "/conftest"); return 1; } } add_constant("testfile", "conftest"); return 0; ]], 1)
457cf02002-11-26Martin Nilsson dnl - file->open dnl - file->close
32cc772004-03-21Henrik Grubbström (Grubba) test_any(object o=Stdio.File(); return o->open(testfile,"wct") && o->close(),1)
7687252008-10-17Martin Stjernholm test_any([[ Stdio.File o = Stdio.File(); o->open ("file_that_doesnt_exist", "r"); return o->errno(); ]], System.ENOENT) test_any([[ Stdio.File o = Stdio.File(); o->open ("file_in_dir/that_doesnt_exist", "r"); return o->errno(); ]], System.ENOENT) test_any([[ Stdio.File o = Stdio.File(); o->open ("file_with_\fu\n\ny_cha\rs", "r"); return o->errno(); ]], System.ENOENT)
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file_stat
32cc772004-03-21Henrik Grubbström (Grubba) test_eq(file_stat(testfile)[1],0)
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - filesystem_stat dnl - rm
32cc772004-03-21Henrik Grubbström (Grubba) test_true(rm(testfile)) test_eq(file_stat(testfile),0)
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->write
32cc772004-03-21Henrik Grubbström (Grubba) test_any(int e; object o=Stdio.File(); if(!o->open(testfile,"wct")) return "open"+o->errno(); e=o->write("sune"); if(!o->close()) return "close"+o->errno(); return e,4)
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->read
32cc772004-03-21Henrik Grubbström (Grubba) test_any(string s; object o=Stdio.File(); if(!o->open(testfile,"r")) return "open"+o->errno(); s=o->read(4); if(!o->close()) return "close"+o->errno(); return s,"sune")
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
32cc772004-03-21Henrik Grubbström (Grubba) test_any(string s; object o=Stdio.File(); if(!o->open(testfile,"r")) return "open"+o->errno(); s=o->read(999999); if(!o->close()) return "close"+o->errno(); return s,"sune")
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
32cc772004-03-21Henrik Grubbström (Grubba) test_any(int e; object o=Stdio.File(); if(!o->open(testfile,"wct")) return "open"+o->errno(); e=o->write(sprintf("%'+-*'100000s","")); if(!o->close()) return "close"+o->errno(); return e,100000)
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
32cc772004-03-21Henrik Grubbström (Grubba) test_any(string s; object o=Stdio.File(); if(!o->open(testfile,"r")) return "open"+o->errno(); s=o->read(9999999); if(!o->close()) return "close"+o->errno(); return s,sprintf("%'+-*'100000s",""))
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
d644c12009-03-15Martin Stjernholm cond_begin([[ Stdio.File()->proxy ]])
32cc772004-03-21Henrik Grubbström (Grubba)  test_any([[string s; object o2,o3,o=Stdio.File(); if(!o->open(testfile,"r")) return "open"+o->errno(); o2=Stdio.File(); o3=o2->pipe(); o2->proxy(o); destruct(o2); s=o3->read(100000); return s]],sprintf("%'+-*'100000s","")) test_any([[string s; object o2,o3,o=Stdio.File(); if(!o->open(testfile,"r")) return "open"+o->errno(); o2=Stdio.File(); o3=o2->pipe(); o2->proxy(o); destruct(o2); s=o3->read(9999999); return s]],sprintf("%'+-*'100000s","")) test_any([[string s; object o2,o3,o=Stdio.File(); if(!o->open(testfile,"r")) return "open"+o->errno(); o2=Stdio.File(); o3=o2->pipe(); o2->proxy(o); o2=0; s=o3->read(9999999); return s]],sprintf("%'+-*'100000s",""))
d3f6b81998-05-05Fredrik Hübinette (Hubbe)  test_any([[ object o2,o=Stdio.File(); o2=o->pipe(); object x2,x=Stdio.File(); x2=x->pipe();
13670c2015-05-25Martin Nilsson 
d3f6b81998-05-05Fredrik Hübinette (Hubbe)  x2->proxy(o); x2=0; switch(o2->write("test")) { case 4: break; default: return "Write failed"; case 0: return "Write failed with errno "+o2->errno()+".\n"; } o2=0; return x->read() || ("errno:"+x->errno()); ]],"test") test_any([[ object o2,o=Stdio.File(); o2=o->pipe(Stdio.PROP_IPC); object x2,x=Stdio.File(); x2=x->pipe();
13670c2015-05-25Martin Nilsson 
d3f6b81998-05-05Fredrik Hübinette (Hubbe)  x2->proxy(o); x2=0; switch(o2->write("test")) { case 4: break; default: return "Write failed"; case 0: return "Write failed with errno "+o2->errno()+".\n"; } o2=0; return x->read() || ("errno:"+x->errno()); ]],"test") test_any([[ object o2,o=Stdio.File(); o2=o->pipe(); object x2,x=Stdio.File(); x2=x->pipe(Stdio.PROP_IPC);
13670c2015-05-25Martin Nilsson 
d3f6b81998-05-05Fredrik Hübinette (Hubbe)  x2->proxy(o); x2=0; switch(o2->write("test")) { case 4: break; default: return "Write failed"; case 0: return "Write failed with errno "+o2->errno()+".\n"; } o2=0; return x->read() || ("errno:"+x->errno()); ]],"test") test_any([[ object o2,o=Stdio.File(); o2=o->pipe(Stdio.PROP_IPC); object x2,x=Stdio.File(); x2=x->pipe(Stdio.PROP_IPC);
13670c2015-05-25Martin Nilsson 
d3f6b81998-05-05Fredrik Hübinette (Hubbe)  x2->proxy(o); x2=0; switch(o2->write("test")) { case 4: break; default: return "Write failed"; case 0: return "Write failed with errno "+o2->errno()+".\n"; } o2=0; return x->read() || ("errno:"+x->errno()); ]],"test")
d644c12009-03-15Martin Stjernholm cond_end // Stdio.File()->proxy
d3f6b81998-05-05Fredrik Hübinette (Hubbe)  test_any([[object o,o2=Stdio.File(); o=o2->pipe(); destruct(o2); return o->read()]],"") test_any([[object o,o2=Stdio.File(); o=o2->pipe(); o2=0; return o->read()]],"") test_any([[object o,o2=Stdio.File(); o=o2->pipe(Stdio.PROP_IPC); destruct(o); return o2->read() || ("error:"+o2->errno())]],"") test_any([[object o,o2=Stdio.File(); o=o2->pipe(Stdio.PROP_IPC); o=0; return o2->read() || ("error:"+o2->errno())]],"")
457cf02002-11-26Martin Nilsson dnl - file->seek dnl - file->tell
32cc772004-03-21Henrik Grubbström (Grubba) test_any(object o=Stdio.File(); return o->open(testfile,"r") && o->read(4711) && o->tell() == 4711 && o->close(),1)
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->stat
32cc772004-03-21Henrik Grubbström (Grubba) test_equal([[Stdio.File(testfile,"r")->stat()[..1]]],[[file_stat(testfile)[..1]]])
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->errno test_do(Stdio.File("stdin")->errno())
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->set_nonblocking dnl - file->set_blocking dnl - file->set_id dnl - file->query_id test_false(Stdio.File("stdin")->query_id())
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - File->query_read_callback test_do(Stdio.File("stdin")->query_read_callback())
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->query_write_callback test_do(Stdio.File("stdin")->query_write_callback())
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->query_close_callback test_do(Stdio.File("stdin")->query_close_callback())
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->open_socket dnl - file->connect dnl - file->query_address dnl - file->pipe test_any([[object o=Stdio.File(),o2=o->pipe();o->write("1"); return o2->read(1)]],"1") test_any([[object o=Stdio.File(),o2=o->pipe();o2->write("1"); return o->read(1)]],"1")
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->dup
32cc772004-03-21Henrik Grubbström (Grubba) test_any([[object o=Stdio.File(); o->open(testfile,"r"); o=o->dup(); return o->read(100)]] ,sprintf("%'+-*'100s",""))
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->assign
32cc772004-03-21Henrik Grubbström (Grubba) test_any([[object o=Stdio.File(),o2=Stdio.File(); o->open(testfile,"r"); o2->assign(o); return o2->read(100)]] ,sprintf("%'+-*'100s",""))
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file->dup2
32cc772004-03-21Henrik Grubbström (Grubba) test_any([[object o=Stdio.File(),o2=Stdio.File(); o2->pipe(); o->open(testfile,"r"); o->dup2(o2); return o2->read(100)]] ,sprintf("%'+-*'100s",""))
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
a12f271998-01-10Fredrik Hübinette (Hubbe) dnl test_eq(Process.popen("echo foo"),"foo\n")
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - socket->bind dnl - socket->set_id dnl - socket->query_id dnl - socket->errno dnl - socket->accept
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) 
32cc772004-03-21Henrik Grubbström (Grubba) test_true(rm(testfile)) test_eq(file_stat(testfile),0) test_any(object o=Stdio.File(); return o->open(testfile,"wac") && o->write("x") && o->close(),1) test_eq(file_stat(testfile)[1],1) test_any(object o=Stdio.File(); return o->open(testfile,"wa") && o->write("x") && o->close(),1) test_eq(file_stat(testfile)[1],2) test_any(object o=Stdio.File(); return o->open(testfile,"wac") && o->write("x") && o->close(),1) test_eq(file_stat(testfile)[1],3) test_any(object o=Stdio.File(); return o->open(testfile,"wa") && o->write("x") && o->close(),1) test_eq(file_stat(testfile)[1],4) test_any(object o=Stdio.File(); return o->open(testfile,"wa") && o->write("x") && o->close(),1) test_eq(file_stat(testfile)[1],5) test_any(object o=Stdio.File(); return o->open(testfile,"war") && o->write("x") && o->close(),1) test_eq(file_stat(testfile)[1],6) test_any(object o=Stdio.File(); return o->open(testfile,"wrac") && o->write("x") && o->close(),1) test_eq(file_stat(testfile)[1],7) test_any(object o=Stdio.File(); return o->open(testfile,"wac") && o->write("x") && o->close(),1) test_eq(file_stat(testfile)[1],8) test_any(object o=Stdio.File(); return o->open(testfile,"wrac") && o->write("x") && o->close(),1) test_eq(file_stat(testfile)[1],9) test_any(object o=Stdio.File(); return o->open(testfile,"wrac") && o->write("x") && o->close(),1) test_eq(file_stat(testfile)[1],10) test_true(rm(testfile)) test_eq(file_stat(testfile),0)
4168fe1998-04-24Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl - file_stat dnl - perror dnl - rm dnl - mkdir dnl - get_dir
b1510a2004-10-24Martin Nilsson test_true(arrayp(get_dir()))
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) test_true(arrayp(get_dir(".")))
457cf02002-11-26Martin Nilsson dnl - cd dnl - getcwd
e5bb7d1996-09-23Fredrik Hübinette (Hubbe) test_true(stringp(getcwd()))
2201d81996-10-15Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl strerror
2ea0e72000-04-08Henrik Grubbström (Grubba) test_do(return strerror(1))
2201d81996-10-15Fredrik Hübinette (Hubbe) test_true(stringp(strerror(2)||""))
32cc772004-03-21Henrik Grubbström (Grubba) test_do(object o=Stdio.File(); if(!o->open(testfile,"wct")) return -1; o->write("foo\n" * 100); o->close();)
f96da01996-10-24Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl Stdio.FILE
32cc772004-03-21Henrik Grubbström (Grubba) test_any([[object o=Stdio.FILE(); o->open(testfile,"r"); return o->gets()]],"foo") test_any(object o=Stdio.FILE(); o->open(testfile,"r"); return o->gets()+o->gets()+o->gets(),"foofoofoo") test_any(int e; object o=Stdio.FILE(); o->open(testfile,"r"); for(e=0;e<100;e++) if(o->gets() != "foo") return e; return -1,-1)
f96da01996-10-24Fredrik Hübinette (Hubbe) 
ab6aec1997-02-11Fredrik Hübinette (Hubbe) test_true(Stdio.stdin) test_true(Stdio.stdout) test_true(Stdio.stderr)
f96da01996-10-24Fredrik Hübinette (Hubbe) 
32cc772004-03-21Henrik Grubbström (Grubba) test_eq(Stdio.read_file(testfile,0,5), "foo\n"*5) test_eq(Stdio.read_file(testfile,1,5), "foo\n"*5)) test_eq(Stdio.read_file(testfile,100,5),"")
d386e61998-03-20Fredrik Hübinette (Hubbe) 
457cf02002-11-26Martin Nilsson dnl locking
d644c12009-03-15Martin Stjernholm cond_begin([[Stdio.File()->lock]])
34965e2004-01-02Henrik Grubbström (Grubba)  dnl FIXME: It seems MacOS X has a flock(2) that always fails with ENOTSUP.
32cc772004-03-21Henrik Grubbström (Grubba)  test_true(Stdio.File(testfile,"wr")->lock()&&1) test_true(Stdio.File(testfile,"wr")->trylock()&&1) test_true(Stdio.File(testfile,"wr")->trylock()&&1)
397b652000-08-27Henrik Grubbström (Grubba)  test_eval_error([[
32cc772004-03-21Henrik Grubbström (Grubba)  mixed o=Stdio.File(testfile,"wr");
1e230a2014-09-14Stephen R. van den Berg  object k=o->lock();
397b652000-08-27Henrik Grubbström (Grubba)  o->lock(); ]]) test_any([[
32cc772004-03-21Henrik Grubbström (Grubba)  mixed o=Stdio.File(testfile,"wr");
397b652000-08-27Henrik Grubbström (Grubba)  object k = o->lock(); return o->trylock(); ]], 0)
32cc772004-03-21Henrik Grubbström (Grubba)  test_true(Stdio.File(testfile,"wr")->lock()&&1)
d644c12009-03-15Martin Stjernholm  cond_end // Stdio.File()->lock
d386e61998-03-20Fredrik Hübinette (Hubbe) 
f53ba32003-05-14Marcus Comstedt dnl pty handling
0e49202003-05-15Marcus Comstedt  cond([[ Stdio.File()->openpt ]], [[ test_true(Stdio.File()->openpt("rw")); ]]) cond([[ Stdio.File()->openpt && Stdio.File()->grantpt ]],
f53ba32003-05-14Marcus Comstedt [[
0e49202003-05-15Marcus Comstedt  test_any([[ Stdio.File p = Stdio.File(); return p->openpt("rw") && stringp(p->grantpt()); ]], 1)
f53ba32003-05-14Marcus Comstedt ]])
32cc772004-03-21Henrik Grubbström (Grubba) test_true(rm(testfile)) test_do(add_constant("testfile"));
3236822015-11-15Martin Nilsson ifefun(testdir,
32cc772004-03-21Henrik Grubbström (Grubba) [[ test_true(rm(testdir)); test_do(add_constant("testdir")); ]])
b294a81998-04-08Fredrik Hübinette (Hubbe) 
2e5db82004-10-21Henrik Grubbström (Grubba) define(run_sub_test, [[
fb4b0d2010-07-11Martin Stjernholm  test_tests([[ array a() {return Tools.Testsuite.run_script ($1);} ]])
2e5db82004-10-21Henrik Grubbström (Grubba) ]])
1682c82015-02-28Arne Goedeke define(run_socktest, [[ run_sub_test($1 + ({"SRCDIR/socktest.pike"})) run_sub_test($1 + ({"-DIPV6", "SRCDIR/socktest.pike"})) run_sub_test($1 + ({"SRCDIR/socktest.pike", "1"})) run_sub_test($1 + ({"-DIPV6", "SRCDIR/socktest.pike", "1"})) run_sub_test($1 + ({"SRCDIR/socktest.pike", "2"})) run_sub_test($1 + ({"-DIPV6", "SRCDIR/socktest.pike", "2"})) ]])
b294a81998-04-08Fredrik Hübinette (Hubbe) 
1682c82015-02-28Arne Goedeke run_socktest(({}))
59feb52005-02-22Henrik Grubbström (Grubba) 
9d9d902013-11-03Per Hedbor cond_begin([[ Pike["SelectBackend"] ]])
1682c82015-02-28Arne Goedeke run_socktest(({"-DBACKEND=SelectBackend"}))
09bfa22008-06-12Henrik Grubbström (Grubba) 
9d9d902013-11-03Per Hedbor cond_end
09bfa22008-06-12Henrik Grubbström (Grubba) 
d644c12009-03-15Martin Stjernholm cond_begin([[ Pike["PollBackend"] ]])
1682c82015-02-28Arne Goedeke  run_socktest(({"-DBACKEND=PollBackend"}))
09bfa22008-06-12Henrik Grubbström (Grubba) 
d644c12009-03-15Martin Stjernholm cond_end
09bfa22008-06-12Henrik Grubbström (Grubba) 
d644c12009-03-15Martin Stjernholm cond_begin([[ Pike["PollDeviceBackend"] ]])
1682c82015-02-28Arne Goedeke  run_socktest(({"-DBACKEND=PollDeviceBackend"}))
09bfa22008-06-12Henrik Grubbström (Grubba) 
d644c12009-03-15Martin Stjernholm cond_end
09bfa22008-06-12Henrik Grubbström (Grubba) 
2e5db82004-10-21Henrik Grubbström (Grubba) run_sub_test(({"SRCDIR/sendfiletest.pike"}))
c3d2922001-04-19Mirar (Pontus Hagland) 
2e5db82004-10-21Henrik Grubbström (Grubba) run_sub_test(({"-DTEST_NORMAL", "SRCDIR/connecttest.pike"})) run_sub_test(({"SRCDIR/connecttest.pike"}))
f710ae2003-03-26Martin Stjernholm  dnl Stdio.Stat test_any([[
68828f2003-03-27Martin Stjernholm  // This actually tests the bignum/64 bit stuff.
f710ae2003-03-26Martin Stjernholm  Stdio.Stat s = Stdio.Stat();
68828f2003-03-27Martin Stjernholm  s->size = 0x100000000; if (s->size != 0x100000000) { werror ("Note: 64 bit file stats apparently not supported on this system.\n"); return 1; }
f710ae2003-03-26Martin Stjernholm  s->size = -0x100000000;
68828f2003-03-27Martin Stjernholm  return s->size == -0x100000000; ]], 1)
3783cd2007-06-19Martin Stjernholm  test_any([[ Stdio.File f = Stdio.File(); return f->query_backend() == Pike.DefaultBackend; ]], 1) test_any([[ Pike.Backend b = Pike.Backend(); Stdio.File f = Stdio.File(); f->set_backend (b); return f->query_backend() == b; ]], 1) test_any([[ Stdio.Port f = Stdio.Port(); return f->query_backend() == Pike.DefaultBackend; ]], 1) test_any([[ Pike.Backend b = Pike.Backend(); Stdio.Port f = Stdio.Port(); f->set_backend (b); return f->query_backend() == b; ]], 1)
cfa6ca2011-10-10Martin Stjernholm 
f39e172012-06-21Martin Nilsson cond_begin([[ Pike["PollDeviceBackend"] && Pike["PollDeviceBackend"]["HAVE_KQUEUE"] ]])
ca6bd02012-05-01Bill Welliver  run_sub_test(({"SRCDIR/kqueuetest.pike"})) cond_end
cb65012014-10-01Martin Nilsson dnl Buffer
fbc3bf2014-09-08Per Hedbor 
cb65012014-10-01Martin Nilsson test_equal([[Stdio.Buffer("hej")->read(1)]], "h")
fbc3bf2014-09-08Per Hedbor  dnl sscanf
cb65012014-10-01Martin Nilsson test_equal([[Stdio.Buffer("hej")->sscanf("%3c")]], ({6841706})) test_equal([[Stdio.Buffer("hej")->match("%3c")]], 6841706)
fbc3bf2014-09-08Per Hedbor  test_any([[ int i;
cb65012014-10-01Martin Nilsson  Stdio.Buffer b=Stdio.Buffer("hej hej hej");
fbc3bf2014-09-08Per Hedbor  while( array a = b->sscanf( "%*[ ]%[^ ]") ) i+= sizeof(a); return i;
f4c93a2014-09-08Per Hedbor ]], 3)
fbc3bf2014-09-08Per Hedbor  dnl range_error dnl output_to() dnl input_from dnl __fd_set_output? dnl _size_object() dnl also tests that size is doubled when adding..
cb65012014-10-01Martin Nilsson test_equal( Stdio.Buffer("hej hej")->_size_object(), 0 ); test_equal( Stdio.Buffer("hej hej")->add("hej")->_size_object(), Stdio.Buffer("hej hej")->add("foo")->add("bar")->_size_object() )
fbc3bf2014-09-08Per Hedbor  dnl sizeof()
cb65012014-10-01Martin Nilsson test_equal( sizeof(Stdio.Buffer("hej")), sizeof("hej")) test_equal( sizeof(Stdio.Buffer("ej")->add("alpha")), 7) test_equal( sizeof(Stdio.Buffer()->sprintf("%4H","hej")), 7)
fbc3bf2014-09-08Per Hedbor  dnl create(int) test_any([[
cb65012014-10-01Martin Nilsson  Stdio.Buffer b = Stdio.Buffer(1024*1024);
fbc3bf2014-09-08Per Hedbor  if( b->_size_object() != 1024*1024 ) return -1; for( int i = 0; i<1024/4*1024; i++ ) b->add("test"); if( b->_size_object() != 1024*1024 ) return -2; if( b->num_malloc ) return -3; if( b->num_move ) return -4; return 1; ]], 1 )
a515e42018-10-09Henrik Grubbström (Grubba) cond_begin([[ System["__MMAP__"] ]])
fbc3bf2014-09-08Per Hedbor dnl create/add( system.memory )
a515e42018-10-09Henrik Grubbström (Grubba)  test_any([[ System.Memory a = System.Memory(__FILE__); System.Memory b = System.Memory(100);
fbc3bf2014-09-08Per Hedbor 
a515e42018-10-09Henrik Grubbström (Grubba)  Stdio.Buffer buf = Stdio.Buffer(a);
fbc3bf2014-09-08Per Hedbor 
a515e42018-10-09Henrik Grubbström (Grubba)  if( buf->_size_object() != 0 ) return -1;
fbc3bf2014-09-08Per Hedbor 
a515e42018-10-09Henrik Grubbström (Grubba)  buf->add( b );
fbc3bf2014-09-08Per Hedbor 
a515e42018-10-09Henrik Grubbström (Grubba)  if( buf->_size_object() < sizeof(a)+sizeof(b) ) return -2; if( !has_suffix((string)buf,"\0"*100) ) return -3; return 1; ]], 1) cond_end
fbc3bf2014-09-08Per Hedbor 
9d3bdd2014-10-01Martin Nilsson dnl create/add( buffer )
f4c93a2014-09-08Per Hedbor test_any([[
cb65012014-10-01Martin Nilsson  Stdio.Buffer a = Stdio.Buffer("buffer 1"); Stdio.Buffer b = Stdio.Buffer("buffer 2");
fbc3bf2014-09-08Per Hedbor 
cb65012014-10-01Martin Nilsson  Stdio.Buffer buf = Stdio.Buffer(a);
fbc3bf2014-09-08Per Hedbor  if( buf->_size_object() < sizeof(a) ) return -1; buf->add( b ); if( buf->_size_object() < sizeof(a)+sizeof(b) ) return -2; if( (string)buf != "buffer 1buffer 2") return -3; return 1;
f4c93a2014-09-08Per Hedbor ]], 1)
fbc3bf2014-09-08Per Hedbor  dnl add( string.buffer )
f4c93a2014-09-08Per Hedbor test_any([[ String.Buffer a = String.Buffer(); String.Buffer b = String.Buffer(); a->add("buffer 1"); b->add("buffer 2");
fbc3bf2014-09-08Per Hedbor 
cb65012014-10-01Martin Nilsson  Stdio.Buffer buf = Stdio.Buffer(a);
fbc3bf2014-09-08Per Hedbor  if( buf->_size_object() < sizeof(a) ) return -1; buf->add( b ); if( buf->_size_object() < sizeof(a)+sizeof(b) ) return -2; if( (string)buf != "buffer 1buffer 2") return -3; return 1;
f4c93a2014-09-08Per Hedbor ]], 1)
fbc3bf2014-09-08Per Hedbor  dnl add( string )
f4c93a2014-09-08Per Hedbor test_any([[
fbc3bf2014-09-08Per Hedbor  string a = ("buffer 1"); string b = ("buffer 2");
cb65012014-10-01Martin Nilsson  Stdio.Buffer buf = Stdio.Buffer(a);
fbc3bf2014-09-08Per Hedbor  if( buf->_size_object() > 0 ) return -1; buf->add( b ); if( buf->_size_object() < sizeof(a)+sizeof(b) ) return -2; if( (string)buf != "buffer 1buffer 2") return -3; return 1;
f4c93a2014-09-08Per Hedbor ]], 1)
fbc3bf2014-09-08Per Hedbor  dnl add( char )
f4c93a2014-09-08Per Hedbor test_any([[
fbc3bf2014-09-08Per Hedbor  string a = ("buffer 1");
cb65012014-10-01Martin Nilsson  Stdio.Buffer buf = Stdio.Buffer(a);
fbc3bf2014-09-08Per Hedbor  if( buf->_size_object() > 0 ) return -1; buf->add( 'b','u','f','f','e','r',' ','2' ); if( buf->_size_object() < sizeof(a)+8 ) return -2; if( (string)buf != "buffer 1buffer 2") return -3; return 1;
f4c93a2014-09-08Per Hedbor ]], 1)
fbc3bf2014-09-08Per Hedbor 
f4c93a2014-09-08Per Hedbor dnl add(array( mix )) test_any([[
fbc3bf2014-09-08Per Hedbor  System.Memory tst = System.Memory(2); tst[0] = ' '; tst[1] = '2';
f4c93a2014-09-08Per Hedbor  String.Buffer er = String.Buffer(); mixed z = ({
cb65012014-10-01Martin Nilsson  'b','u','f',Stdio.Buffer("f"),
f4c93a2014-09-08Per Hedbor  er,
fbc3bf2014-09-08Per Hedbor  tst
f4c93a2014-09-08Per Hedbor  }); er->add("er");
fbc3bf2014-09-08Per Hedbor 
cb65012014-10-01Martin Nilsson  Stdio.Buffer buf = Stdio.Buffer("buffer 1");
fbc3bf2014-09-08Per Hedbor  if( buf->_size_object() > 0 ) return -1;
f4c93a2014-09-08Per Hedbor  buf->add( z, 'b','u','f','f',({'e','r',({' '}),'3'}) );
fbc3bf2014-09-08Per Hedbor 
f4c93a2014-09-08Per Hedbor  if( buf->_size_object() < 24 )
fbc3bf2014-09-08Per Hedbor  return -2;
f4c93a2014-09-08Per Hedbor  if( (string)buf != "buffer 1buffer 2buffer 3")
fbc3bf2014-09-08Per Hedbor  return -3; return 1;
f4c93a2014-09-08Per Hedbor ]], 1)
fbc3bf2014-09-08Per Hedbor  dnl multi add( combo, also sort of a speed test ) dnl will use on average 3Mb RAM
f4c93a2014-09-08Per Hedbor test_any([[ String.Buffer two = String.Buffer();
fbc3bf2014-09-08Per Hedbor  array tst = ({ System.Memory(1),
f4c93a2014-09-08Per Hedbor  two,
fbc3bf2014-09-08Per Hedbor  '3', "4",
cb65012014-10-01Martin Nilsson  Stdio.Buffer("5"),
f4c93a2014-09-08Per Hedbor  ({ "6", ({'7',"","",""}) }),
fbc3bf2014-09-08Per Hedbor  });
f4c93a2014-09-08Per Hedbor  two->add("2");
fbc3bf2014-09-08Per Hedbor  int l = 0;
cb65012014-10-01Martin Nilsson  Stdio.Buffer res = Stdio.Buffer();
f4c93a2014-09-08Per Hedbor  for( int i = 0; i<102*1024; i++ )
fbc3bf2014-09-08Per Hedbor  {
f4c93a2014-09-08Per Hedbor  array args = allocate( random(50) );
fbc3bf2014-09-08Per Hedbor  for( int j=0; j<sizeof( args ); j++ ) {
f4c93a2014-09-08Per Hedbor  mixed e = random( tst ); if( intp( e ) ) l +=1; else l += sizeof( e ); args[j] = e;
fbc3bf2014-09-08Per Hedbor  } res->add(@args);
f4c93a2014-09-08Per Hedbor  if( sizeof( res ) != l ) return ([i:args, "l":l,"sizeof":sizeof(res)]);
fbc3bf2014-09-08Per Hedbor  } return 1;
f4c93a2014-09-08Per Hedbor ]], 1)
fbc3bf2014-09-08Per Hedbor  dnl basic zero-copy check test_any( [[
cb65012014-10-01Martin Nilsson  Stdio.Buffer i = Stdio.Buffer("zero copy");
fbc3bf2014-09-08Per Hedbor  if( i->num_malloc ) return -1; if( i->_size_object() != 0 ) return -2; i->add(" data goes here"); if( i->num_malloc!= 1 ) return -3; if( i->_size_object() == 0 ) return -4; i->read(); if( i->_size_object() == 0 ) return -5; i->add("zero copy");
8ad8752015-07-26Henrik Grubbström (Grubba)  // NB: This was "string" in previous versions of Stdio.Buffer. if( !has_value(sprintf("%O",i), "allocated") ) return -6;
fbc3bf2014-09-08Per Hedbor  i->add(" indeed"); /* note: shorter than first string. */
f4c93a2014-09-08Per Hedbor  if( i->_size_object() == 0 ) return -7;
fbc3bf2014-09-08Per Hedbor  if( i->num_malloc != 1 ) return -8;
f4c93a2014-09-08Per Hedbor  /*
8ad8752015-07-26Henrik Grubbström (Grubba)  create - no move
f4c93a2014-09-08Per Hedbor  add - realloc + move read - no move (buffer now empty) add - point to str add - move to malloc arena add - no move */ return i->num_move <= 2; ]], 1)
fbc3bf2014-09-08Per Hedbor 
74c4102014-09-14Martin Nilsson dnl add_int8()
fbc3bf2014-09-08Per Hedbor 
f4c93a2014-09-08Per Hedbor test_any( [[
cb65012014-10-01Martin Nilsson  Stdio.Buffer b = Stdio.Buffer();
f4c93a2014-09-08Per Hedbor  for( int i=0; i<255; i++ )
74c4102014-09-14Martin Nilsson  b->add_int8( i )->add_int8(~i);
f4c93a2014-09-08Per Hedbor  for( int i=0; i<255; i++ ) if( b[i*2] != i || b[i*2+1] != (~i&255)) return -i; return 1; ]], 1 )
5fcfe82014-09-14Stephen R. van den Berg dnl add_int16()
fbc3bf2014-09-08Per Hedbor 
9fc2ef2014-09-08Per Hedbor  test_any( [[ string ref = "";
cb65012014-10-01Martin Nilsson  Stdio.Buffer b = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  for( int i=0; i<255; i++ ) { ref += sprintf("%2c%2c", i, (~i)&0xffff );
5fcfe82014-09-14Stephen R. van den Berg  b->add_int16( i )->add_int16(~i);
9fc2ef2014-09-08Per Hedbor  if( (string)b != ref ) return ([ "i":i, "ref":ref, "b":(string)b ]); } return 1; ]], 1 )
f4c93a2014-09-08Per Hedbor 
fbc3bf2014-09-08Per Hedbor dnl add_int32()
9fc2ef2014-09-08Per Hedbor  test_any( [[ string ref = "";
cb65012014-10-01Martin Nilsson  Stdio.Buffer b = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  for( int i=0; i<255; i++ ) { ref += sprintf("%4c%4c", i, (~i)&0xffffffff ); b->add_int32( i )->add_int32(~i); if( (string)b != ref ) return ([ "i":i, "ref":ref, "b":(string)b ]); } return 1; ]], 1 )
fbc3bf2014-09-08Per Hedbor  dnl add_int( x, bits);
9fc2ef2014-09-08Per Hedbor  test_any( [[ string ref = "";
cb65012014-10-01Martin Nilsson  Stdio.Buffer b = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  for( int j=1; j<255; j++ ) { string fmt = "%"+j+"c"; for( int i=0; i<255; i+=random(10) ) { ref += sprintf(fmt, i ); b->add_int( i, j ); } if( (string)b != ref ) return ([ "width":j, "ref":ref, "b":(string)b ]); b->clear(); ref = ""; } return 1; ]], 1 )
fbc3bf2014-09-08Per Hedbor dnl add_int( bignum, bits);
9fc2ef2014-09-08Per Hedbor test_any( [[ string ref = "";
cb65012014-10-01Martin Nilsson  Stdio.Buffer b = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  for( int j=1; j<255; j++ ) { int i = random((1<<(j*8))-1); ref += sprintf("%"+j+"c", i ); b->add_int( i, j ); if( (string)b != ref ) return ([ "i":i, "j":j, "ref":ref, "b":(string)b ]); } return 1; ]], 1 ) dnl add_hstring( str, bytes ) test_error( [[
cb65012014-10-01Martin Nilsson  Stdio.Buffer b = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  b->add_hstring("testing", 0 ); ]], 1 ) test_error( [[
cb65012014-10-01Martin Nilsson  Stdio.Buffer b = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  b->add_hstring(" "*256, 1 ); ]], 1 ) test_any( [[
cb65012014-10-01Martin Nilsson  array(Stdio.Buffer) b = allocate(10,Stdio.Buffer)();
9fc2ef2014-09-08Per Hedbor  for( int i = 0; i<1000; i++ ) { string chunk = " "*random(255); for( int w = 0; w<9; w++ ) b[w]->add_hstring( chunk, w+1 ); } for( int i = 0; i<1000; i++ ) { string chunk = b[0]->read_hstring(1); for( int w = 1; w<9; w++ ) { if( b[w]->read_hstring( w+1 ) != chunk ) return ({w+1,strlen(chunk)}); } } return 1; ]], 1); test_any( [[
cb65012014-10-01Martin Nilsson  Stdio.Buffer b = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  b->add( "\0\1" ); if( b->read_hstring( 2 ) ) return -1; if( sizeof(b) != 2 ) return -2; b->add("x"); if( b->read_hstring( 2 ) != "x" ) return -3; return 1; ]], 1);
fbc3bf2014-09-08Per Hedbor  dnl add_hstring( obj*, bits )
9fc2ef2014-09-08Per Hedbor test_any( [[
cb65012014-10-01Martin Nilsson  array(Stdio.Buffer) b = allocate(10,Stdio.Buffer)();
9fc2ef2014-09-08Per Hedbor  System.Memory chunk = System.Memory(255); for( int i = 0; i<1000; i++ ) { for( int w = 0; w<9; w++ ) b[w]->add_hstring( chunk, w+1 ); } for( int i = 0; i<1000; i++ ) { string chunk = b[0]->read_hstring(1); for( int w = 1; w<9; w++ ) { if( b[w]->read_hstring( w+1 ) != chunk ) return ({w+1,strlen(chunk)}); } } return 1; ]], 1);
fbc3bf2014-09-08Per Hedbor dnl add_ints
9fc2ef2014-09-08Per Hedbor test_any( [[ array q = allocate(100000,random)(8438439834983948938439849834983498349834983498);
cb65012014-10-01Martin Nilsson  Stdio.Buffer i = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  i->add_ints( q, 20 ); return 1; ]], 1); test_error( [[ array q = ({8438439834983948938439849834983498349834983498});
cb65012014-10-01Martin Nilsson  Stdio.Buffer i = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  i->add_ints( q, 7 ); return 1; ]], 1); test_any( [[ array q = ({10,20,30});
cb65012014-10-01Martin Nilsson  Stdio.Buffer i = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  i->add_ints( q, 7 ); return 1; ]], 1); test_error( [[ array q = ({10,"20",30});
cb65012014-10-01Martin Nilsson  Stdio.Buffer i = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  i->add_ints( q, 7 ); return 1; ]], 1); test_any( [[ array q = ({10,"20",30});
cb65012014-10-01Martin Nilsson  Stdio.Buffer i = Stdio.Buffer();
9fc2ef2014-09-08Per Hedbor  catch{i->add_ints( q, 7 );}; if( sizeof(i) ) return -1; return 1; ]], 1);
fbc3bf2014-09-08Per Hedbor dnl lock dnl cast
5d679b2014-12-01Martin Nilsson test_any([[ Stdio.Buffer b = Stdio.Buffer("a\0b"); return (string)b+(string)b; ]], "a\0ba\0b") test_any([[ class A { inherit Stdio.Buffer; mixed cast(string to) { if(to=="int") return 7; return ::cast(to); } }; Stdio.Buffer b = A("x"); return (string)b+(int)b; ]], "x7")
fbc3bf2014-09-08Per Hedbor dnl indexing dnl set_error_mode dnl _sprintf
20d4c82014-11-27Henrik Grubbström (Grubba) test_any( [[ // Test that _sprintf() doesn't modify the buffer, // and that the characters are qouted as expected. // // NB: The formatting generated by %O is not normative, // and may change. Stdio.Buffer buf = Stdio.Buffer("ABC\"\n\0"); return sprintf("%O|%s|%q|%s|%q|%O|", buf, buf, buf, buf, buf, buf) + (string)buf; ]], "_static_modules._Stdio()->Buffer(6 bytes, read=[..-1] data=[0..5] free=[6..0] string)|" "ABC\"\n\0|" "\"ABC\\\"\\n\\0\"|" "ABC\"\n\0|" "\"ABC\\\"\\n\\0\"|" "_static_modules._Stdio()->Buffer(6 bytes, read=[..-1] data=[0..5] free=[6..0] string)|" "ABC\"\n\0")
ca6bd02012-05-01Bill Welliver 
f4d37e2015-04-13Martin Nilsson dnl rewind_key.rewind test_any([[ Stdio.Buffer b = Stdio.Buffer("123456789"); if( b->read(2) != "12" ) return 1; Stdio.Buffer.RewindKey k = b->rewind_key(); if( b->read(2) != "34" ) return 2; k->rewind(); if( b->read(4) != "3456") return 3; return -1; ]], -1) test_any([[ Stdio.Buffer b = Stdio.Buffer("123456789"); Stdio.Buffer.RewindKey k1 = b->rewind_key(); if( b->read(6) != "123456" ) return 1; Stdio.Buffer.RewindKey k2 = b->rewind_key(); k1->rewind(); if( b->read(1) != "1" ) return 2; k2->rewind(); if( b->read(1) != "7" ) return 3; return -1; ]], -1) test_any([[ Stdio.Buffer b = Stdio.Buffer("123"); Stdio.Buffer.RewindKey k = b->rewind_key(); b->read(1); k->rewind(); // Destructs the key. return !!k; ]], 0)
5abd602015-10-25Martin Nilsson test_any([[ Stdio.Buffer b = Stdio.Buffer("\0\0a\0bb\nccc"); if( b->read_cstring()!="" ) return 1; if( b->read_cstring()!="" ) return 2; if( b->read_cstring()!="a" ) return 3; if( b->read_cstring('\n')!="bb" ) return 4; if( !undefinedp(b->read_cstring()) ) return 5; if( b->read()!="ccc" ) return 6; return -1; ]], -1)
7dfcee2016-10-17Martin Nilsson dnl search
940a5e2016-11-05Martin Nilsson test_any_equal([[ Stdio.Buffer b = Stdio.Buffer("foobar"); return ({ search(b, 'f'), search(b, 'o'), search(b, 'x'), search(b, 0), search(b, -1), search(b, 'o', 1), search(b, 'o', 2), search(b, 'o', 0, 0), search(b, 'o', 0, 3) }); ]], ({ 0, 1, -1, -1, -1, 1, 2, -1, 1 }))
7dfcee2016-10-17Martin Nilsson test_any([[ Stdio.Buffer b = Stdio.Buffer("hejhopp"); return search(b, "he"); ]], 0) test_any([[ Stdio.Buffer b = Stdio.Buffer("hejhopp"); return search(b, "ho"); ]], 3) test_any([[ Stdio.Buffer b = Stdio.Buffer("hejhopp"); return search(b, "hi");
940a5e2016-11-05Martin Nilsson ]], -1)
7dfcee2016-10-17Martin Nilsson test_any([[ Stdio.Buffer b = Stdio.Buffer(""); return search(b, ""); ]], 0) test_any([[ Stdio.Buffer b = Stdio.Buffer("hejhopphej"); b->read(1); return search(b, "he"); ]], 6) test_any([[ Stdio.Buffer b = Stdio.Buffer("hejhopphej"); return search(b, "he", 1); ]], 7) test_any([[ Stdio.Buffer b = Stdio.Buffer("hejhopp"); return search(b, "ho", 0, 3);
940a5e2016-11-05Martin Nilsson ]], -1)
7cd96d2017-05-07Martin Nilsson test_any([[ return Stdio.Buffer("\1\2\3\4")->read_int(1); ]], 0x01) test_any([[ return Stdio.Buffer("\1\2\3\4")->read_int(2); ]], 0x0102) test_any([[ return Stdio.Buffer("\1\2\3\4")->read_int(3); ]], 0x010203) test_any([[ return Stdio.Buffer("\1\2\3\4")->read_int(4); ]], 0x01020304) test_any([[ return Stdio.Buffer("\1\2\3\4")->read_le_int(1); ]], 0x01) test_any([[ return Stdio.Buffer("\1\2\3\4")->read_le_int(2); ]], 0x0201) test_any([[ return Stdio.Buffer("\1\2\3\4")->read_le_int(3); ]], 0x030201) test_any([[ return Stdio.Buffer("\1\2\3\4")->read_le_int(4); ]], 0x04030201)
7dfcee2016-10-17Martin Nilsson 
1e6a592017-07-30Arne Goedeke dnl unread test_any( [[ /* The documentation promises that we can always unread() */ Stdio.Buffer b = Stdio.Buffer(); b->add("0" * 90); b->add(" " * 10); b->read(90); if (b->unread(90) != 0) return 1; b->sscanf("%d"); if (b->unread(90) != 0) return 2; b->read_buffer(90); if (b->unread(90) != 0) return 3; b->match("%d"); if (b->unread(90) != 0) return 4; b->consume(90); if (b->unread(90) != 0) return 5; return 0; ]], 0)
1169d92004-05-02Martin Nilsson END_MARKER