Skip to content

Commit

Permalink
more comprehensive fileutils testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tkittel committed Oct 30, 2024
1 parent dd2b97a commit 9534551
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 24 deletions.
21 changes: 21 additions & 0 deletions tests/modules/lib_testfileutils/lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ NCTEST_CTYPE_DICTIONARY
"int nctest_file_exists( const char * );"
"const char * nctest_ncgetcwd();"
"const char * nctest_readEntireFileToString( const char * );"
"const char * nctest_ncglob( const char * );"
;
}

Expand Down Expand Up @@ -77,3 +78,23 @@ NCTEST_CTYPES const char * nctest_ncgetcwd()
return &buf[0];
}


NCTEST_CTYPES const char * nctest_ncglob(const char * pattern)
{
//For testing, we can get away with just using a large static buffer:
static char buf[10485760];//10MB, plenty!
try {
std::ostringstream ss;
bool first = true;
for (auto&e : NC::ncglob(pattern) ) {
if ( !first )
ss << "<<@>>";//separator
first = false;
ss << e;
}
buf[0] = '\0';
std::strncat(buf,ss.str().c_str(),sizeof(buf)-1);
} NCCATCH;
return &buf[0];
}

6 changes: 6 additions & 0 deletions tests/pypath/NCTestUtils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,9 @@ def _resolve_print_fct(printfct):
return biprint
else:
return printfct

def explicit_unicode_char(c):
#32 is space, <32 are control chars, 127 is DEL.
return c if 32<=ord(c)<=126 else r'\u{%i}'%ord(c)
def explicit_unicode_str(s):
return ''.join( explicit_unicode_char(c) for c in s)
4 changes: 2 additions & 2 deletions tests/pypath/NCTestUtils/loadlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def add( self, fctname, restype, *argtypes ):
@property
def functions(self):
"""Get the name of all available functions in this library"""
return [f for f,_,_ in self.__fcts]
return sorted([f for f,_,_ in self.__fcts])

def dump(self,prefix=''):
"""Print available functions in this library"""
Expand All @@ -94,7 +94,7 @@ def dump(self,prefix=''):
len(self.__fcts)))
if not self.__fcts:
print(f"{prefix} <no functions defined>")
for fctname,restype,argtypes in self.__fcts:
for fctname,restype,argtypes in sorted(self.__fcts):
rt = _ctype_2_str(restype)
a=', '.join([_ctype_2_str(e) for e in argtypes])
print(f"{prefix} {rt} {fctname}({a})")
Expand Down
13 changes: 13 additions & 0 deletions tests/scripts/fileutils.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Library "TestMod_testfileutils" (4 functions):
int nctest_file_exists(const char *)
const char * nctest_ncgetcwd()
const char * nctest_ncglob(const char *)
const char * nctest_readEntireFileToString(const char *)
Testing ncgetcwd...
...ncgetcwd ok
READ> 'some'
READ> 'multi'
READ> 'line text'
ncglob('unicode*/*.ncmat') -> ['unicodedir_test\\u{17664}abc/b\\u{955}.ncmat']
ncglob('unicode*/*.txt') -> ['unicodedir_test\\u{17664}abc/a.txt', 'unicodedir_test\\u{17664}abc/b.txt']
ncglob('*abc/*\\u{955}*') -> ['unicodedir_test\\u{17664}abc/b\\u{955}.ncmat']
84 changes: 62 additions & 22 deletions tests/scripts/fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,73 @@

import NCTestUtils.enable_fpe
from NCTestUtils.loadlib import Lib
from NCTestUtils.common import explicit_unicode_str
import pathlib

lib = Lib('testfileutils')
lib.dump()
assert hasattr(lib,'nctest_file_exists')
assert hasattr(lib,'nctest_ncgetcwd')
assert hasattr(lib,'nctest_readEntireFileToString')
assert hasattr(lib,'nctest_ncglob')

_raw_ncglob = lib.nctest_ncglob
def nctest_ncglob( pattern ):
return _raw_ncglob(pattern).split('<<@>>')
lib.nctest_ncglob = nctest_ncglob

def test1():
d = pathlib.Path('.')
(d / 'foo.txt').write_text('foo')

assert lib.nctest_file_exists('./foo.txt')
assert lib.nctest_file_exists('foo.txt')
assert not lib.nctest_file_exists('bar.txt')
assert not lib.nctest_file_exists('./bar.txt')
assert not lib.nctest_file_exists('/some/file/that/does/not/exist')
print("Testing ncgetcwd...")
_cwd = lib.nctest_ncgetcwd()
d_abs = d.resolve().absolute()
assert d.samefile(d_abs)
assert d_abs.samefile ( pathlib.Path(_cwd).resolve().absolute() )
assert d_abs.samefile ( _cwd )
assert d.samefile ( _cwd )
print("...ncgetcwd ok")

def test2():
dirname = 'unicodedir_test\u4500abc'
subdir = pathlib.Path('.') / dirname
subdir.mkdir()
for fn in [ 'a.txt', 'b.txt', 'b\u03bb.ncmat' ]:
(subdir / fn).write_text('dummy')
testtext = 'some\nmulti\nline text\n'
(subdir / 'b\u03bb.ncmat').write_text(testtext)

content = lib.nctest_readEntireFileToString(
'unicodedir_test\u4500abc/b\u03bb.ncmat'
)
assert testtext == content
for e in content.splitlines():
print('READ>',repr(e))

def testglob(pattern,nexpect):
g = lib.nctest_ncglob(pattern)
assert len(g) == nexpect
g = list(explicit_unicode_str(e) for e in g)
print(f'ncglob({repr(explicit_unicode_str(pattern))}) -> {repr(g)}')
testglob('unicode*/*.ncmat',1)
testglob('unicode*/*.txt',2)
testglob('*abc/*\u03bb*',1)

def main():


test1()
test2()

if __name__=='__main__':
main()

import pathlib
d = pathlib.Path('.')
(d / 'foo.txt').write_text('foo')

assert lib.nctest_file_exists('./foo.txt')
assert lib.nctest_file_exists('foo.txt')
assert not lib.nctest_file_exists('bar.txt')
assert not lib.nctest_file_exists('./bar.txt')
assert not lib.nctest_file_exists('/some/file/that/does/not/exist')
print("Testing that two files are identical:")
_cwd = lib.nctest_ncgetcwd()
d_abs = d.resolve().absolute()
assert d.samefile(d_abs)
print(" 1) %s"%d_abs)
print(" 2) %s"%_cwd)
assert d_abs.samefile ( pathlib.Path(_cwd).resolve().absolute() )
assert d_abs.samefile ( _cwd )
assert d.samefile ( _cwd )

#FIXME : Step into dir with on-ascii name and check ncgetcwd ??

#print('cwd',lib.nctest_ncgetcwd())

#FIXME: Much more, including globbing, is_absolute_path("c:[\]bla.ncmat) and
#whatever is likely to cause issues on Windows.
1 change: 1 addition & 0 deletions tests/scripts/vdosgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def main():

PWLinDistMoments.unit_test(mp)

#Fixme: add many more here, so we have good coverage.
for filename in ('Au_sg225',
'Polyethylene_CH2'):
for temp in (10,600):
Expand Down

0 comments on commit 9534551

Please sign in to comment.