⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_ncbifile.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * =========================================================================== * PRODUCTION $Log: test_ncbifile.cpp,v $ * PRODUCTION Revision 1000.5  2004/06/01 19:10:13  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.38 * PRODUCTION * =========================================================================== *//*  $Id: test_ncbifile.cpp,v 1000.5 2004/06/01 19:10:13 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software/database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software/database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Vladimir Ivanov * * File Description:   Test program for file's accessory functions * */#include <ncbi_pch.hpp>#include <corelib/ncbiapp.hpp>#include <corelib/ncbiargs.hpp>#include <corelib/ncbifile.hpp>#include <corelib/ncbitime.hpp>#include <stdio.h>#include <test/test_assert.h>  /* This header must go last */USING_NCBI_SCOPE;/////////////////////////////////// File name spliting tests//static void s_TEST_SplitPath(void){    string path, dir, title, ext;    #if defined(NCBI_OS_MSWIN)    CFile::SplitPath("c:\\windows\\command\\win.ini", &dir, &title, &ext);    assert( dir   == "c:\\windows\\command\\" );    assert( title == "win" );    assert( ext   == ".ini" );    path = CFile::MakePath("c:\\windows\\", "win.ini");    assert( path == "c:\\windows\\win.ini" );    CFile::SplitPath("c:win.ini", &dir, &title, &ext);    assert( dir   == "c:" );    assert( title == "win" );    assert( ext   == ".ini" );    path = CFile::MakePath("c:", "win", "ini");    assert( path == "c:win.ini" );    CFile f("c:\\dir\\subdir\\file.ext");    assert( f.GetDir()  == "c:\\dir\\subdir\\" );    assert( f.GetName() == "file.ext" );    assert( f.GetBase() == "file" );    assert( f.GetExt()  == ".ext" );    assert( CFile::AddTrailingPathSeparator("dir")   == "dir\\");    assert( CFile::AddTrailingPathSeparator("dir\\") == "dir\\");    assert( CFile::DeleteTrailingPathSeparator("dir\\path\\/")=="dir\\path");#elif defined(NCBI_OS_UNIX)    CFile::SplitPath("/usr/lib/any.other.lib", &dir, &title, &ext);    assert( dir   == "/usr/lib/" );    assert( title == "any.other" );    assert( ext   == ".lib" );        path = CFile::MakePath("/dir/subdir", "file", "ext");    assert( path == "/dir/subdir/file.ext" );    CFile f("/usr/lib/any.other.lib");    assert( f.GetDir()  == "/usr/lib/" );    assert( f.GetName() == "any.other.lib" );    assert( f.GetBase() == "any.other" );    assert( f.GetExt()  == ".lib" );    assert( CFile::AddTrailingPathSeparator("dir")  == "dir/");    assert( CFile::AddTrailingPathSeparator("dir/") == "dir/");    assert( CFile::DeleteTrailingPathSeparator("dir/path////")=="dir/path");#elif defined(NCBI_OS_MAC)	CFile::SplitPath("Hard Disk:Folder:1984.mov", &dir, &title, &ext);    assert( dir  == "Hard Disk:Folder:" );    assert( title == "1984" );    assert( ext  == ".mov" );        path = CFile::MakePath("Hard Disk:Folder:",  "file", "ext");    assert( path == "Hard Disk:Folder:file.ext" );    path = CFile::MakePath("Hard Disk:Folder",   "file", "ext");    assert( path == "Hard Disk:Folder:file.ext" );    assert( CFile::AddTrailingPathSeparator("dir")  == "dir:");    assert( CFile::AddTrailingPathSeparator("dir:") == "dir:");    assert( CFile::DeleteTrailingPathSeparator("dir:path:::") == "dir:path");#endif    CFile::SplitPath("name", &dir, &title, &ext);    assert( dir.empty() );    assert( title == "name" );    assert( ext.empty() );    path = CFile::MakePath(dir, title, ext);    assert( path == "name" );    CFile::SplitPath(kEmptyStr, &dir, &title, &ext);    assert( dir.empty() );    assert( title.empty() );    assert( ext.empty() );    path = CFile::MakePath(dir, title, ext);    assert( path.empty() );}///////////////////////////////////  Path checking tests//static void s_TEST_CheckPath(void){     CDirEntry d;    // IsAbsolutePath() test#if defined(NCBI_OS_MSWIN)    assert( d.IsAbsolutePath("c:\\") );    assert( d.IsAbsolutePath("c:\\file") );    assert( d.IsAbsolutePath("c:file") );    assert( d.IsAbsolutePath("\\\\machine\\dir") );    assert( d.IsAbsolutePath("\\file") );    assert(!d.IsAbsolutePath("file") );    assert(!d.IsAbsolutePath(".\\file") );    assert(!d.IsAbsolutePath("..\\file") );    assert(!d.IsAbsolutePath("dir\\file") );#elif defined(NCBI_OS_UNIX)    assert( d.IsAbsolutePath("/") );    assert( d.IsAbsolutePath("/file") );    assert( d.IsAbsolutePath("/dir/dir") );    assert(!d.IsAbsolutePath("file") );    assert(!d.IsAbsolutePath("./file") );    assert(!d.IsAbsolutePath("../file") );    assert(!d.IsAbsolutePath("dir/file") );#elif defined(NCBI_OS_MAC)    assert( d.IsAbsolutePath("HD:") );    assert( d.IsAbsolutePath("HD:file") );    assert(!d.IsAbsolutePath("file") );    assert(!d.IsAbsolutePath(":file") );    assert(!d.IsAbsolutePath(":file:file") );#endif    // Convert path to OS dependent test    assert( d.ConvertToOSPath("")               == "" );    assert( d.ConvertToOSPath("c:\\file")       == "c:\\file" );    assert( d.ConvertToOSPath("/dir/file")      == "/dir/file" );    assert( d.ConvertToOSPath("dir:file")       == "dir:file" );#if defined(NCBI_OS_MSWIN)    assert( d.ConvertToOSPath("dir")            == "dir" );    assert( d.ConvertToOSPath("dir\\file")      == "dir\\file" );    assert( d.ConvertToOSPath("dir/file")       == "dir\\file" );    assert( d.ConvertToOSPath(":dir:file")      == "dir\\file" );    assert( d.ConvertToOSPath(":dir::file")     == "file" );    assert( d.ConvertToOSPath(":dir:::file")    == "..\\file" );    assert( d.ConvertToOSPath("./dir/file")     == "dir\\file" );    assert( d.ConvertToOSPath("../file")        == "..\\file" );    assert( d.ConvertToOSPath("../../file")     == "..\\..\\file" );#elif defined(NCBI_OS_UNIX)    assert( d.ConvertToOSPath("dir")            == "dir" );    assert( d.ConvertToOSPath("dir\\file")      == "dir/file" );    assert( d.ConvertToOSPath("dir/file")       == "dir/file" );    assert( d.ConvertToOSPath(":dir:file")      == "dir/file" );    assert( d.ConvertToOSPath(":dir::file")     == "file" );    assert( d.ConvertToOSPath(":dir:::file")    == "../file" );    assert( d.ConvertToOSPath(".\\dir\\file")   == "dir/file" );    assert( d.ConvertToOSPath("..\\file")       == "../file" );    assert( d.ConvertToOSPath("..\\..\\file")   == "../../file" );#elif defined(NCBI_OS_MAC)    assert( d.ConvertToOSPath("dir")            == ":dir" );    assert( d.ConvertToOSPath("dir\\file")      == ":dir:file" );    assert( d.ConvertToOSPath("dir/file")       == ":dir:file" );    assert( d.ConvertToOSPath(":dir:file")      == ":dir:file" );    assert( d.ConvertToOSPath("./dir/file")     == ":dir:file" );    assert( d.ConvertToOSPath("../file")        == "::file" );    assert( d.ConvertToOSPath("../../file")     == ":::file" );    assert( d.ConvertToOSPath("../.././../file")== "::::file" );#endif    // ConcatPath() test#if defined(NCBI_OS_MSWIN)    assert( d.ConcatPath("c:", "file")          == "c:file" );    assert( d.ConcatPath("dir", "file")         == "dir\\file" );    assert( d.ConcatPath("dir", "\\file")       == "dir\\file" );    assert( d.ConcatPath("dir\\", "file")       == "dir\\file" );    assert( d.ConcatPath("\\dir\\", "file")     == "\\dir\\file" );    assert( d.ConcatPath("", "file")            == "file" );    assert( d.ConcatPath("dir", "")             == "dir\\" );    assert( d.ConcatPath("", "")                == "" );#elif defined(NCBI_OS_UNIX)    assert( d.ConcatPath("dir", "file")         == "dir/file" );    assert( d.ConcatPath("dir", "/file")        == "dir/file" );    assert( d.ConcatPath("dir/", "file")        == "dir/file" );    assert( d.ConcatPath("/dir/", "file")       == "/dir/file" );    assert( d.ConcatPath("", "file")            == "file" );    assert( d.ConcatPath("dir", "")             == "dir/" );    assert( d.ConcatPath("", "")                == "" );#elif defined(NCBI_OS_MAC)    assert( d.ConcatPath("HD", "dir")           == "HD:dir" );    assert( d.ConcatPath(":dir", "file")        == ":dir:file" );    assert( d.ConcatPath("dir:", "file")        == "dir:file" );    assert( d.ConcatPath("dir", ":file")        == "dir:file" );    assert( d.ConcatPath("dir::", "file")       == "dir::file" );    assert( d.ConcatPath("dir", "::file")       == "dir::file" );    assert( d.ConcatPath("", "file")            == ":file" );    assert( d.ConcatPath(":file", "")           == ":file:" );    assert( d.ConcatPath("", "")                == ":" );#endif    // Concat any OS paths    assert( d.ConcatPathEx("dir/", "file")      == "dir/file" );    assert( d.ConcatPathEx("/dir/", "file")     == "/dir/file" );    assert( d.ConcatPathEx("dir\\", ":file")    == "dir\\file" );    assert( d.ConcatPathEx("dir\\dir", "file")  == "dir\\dir\\file" );    assert( d.ConcatPathEx("dir/", ":file")     == "dir/file" );    assert( d.ConcatPathEx("dir/dir", "file")   == "dir/dir/file" );    assert( d.ConcatPathEx("dir:dir", "file")   == "dir:dir:file" );    // Normalize path test#if defined(NCBI_OS_MSWIN)    assert( d.NormalizePath("c:\\")             == "c:\\" );    assert( d.NormalizePath("c:\\file")         == "c:\\file" );    assert( d.NormalizePath("c:file")           == "c:file" );    assert( d.NormalizePath("c:\\dir\\..\\file")== "c:\\file" );    assert( d.NormalizePath("c:..\\file")       == "c:..\\file" );    assert( d.NormalizePath("..\\file")         == "..\\file" );    assert( d.NormalizePath("\\..\\file")       == "\\file" );    assert( d.NormalizePath(".\\..\\dir\\..")   == ".." );    assert( d.NormalizePath(".\\dir\\.")        == "dir" );    assert( d.NormalizePath(".\\.\\.\\.")       == "." );    assert( d.NormalizePath("..\\..\\..\\..")   == "..\\..\\..\\.." );    assert( d.NormalizePath("dir\\\\dir\\\\")   == "dir\\dir" );    assert( d.NormalizePath("\\\\machine\\dir") == "\\\\machine\\dir");    assert( d.NormalizePath("\\\\?\\x")         == "\\x" );    assert( d.NormalizePath("\\\\?\\UNC\\m\\d") == "\\\\m\\d" );    assert( d.NormalizePath("dir/file")         == "dir\\file" );#elif defined(NCBI_OS_UNIX )    assert( d.NormalizePath("/file")            == "/file" );    assert( d.NormalizePath("./file")           == "file" );    assert( d.NormalizePath("dir1/dir2/../file")== "dir1/file" );    assert( d.NormalizePath("../file")          == "../file" );    assert( d.NormalizePath("/../file")         == "/file" );    assert( d.NormalizePath("./../dir/..")      == ".." );    assert( d.NormalizePath("./dir/.")          == "dir" );    assert( d.NormalizePath("./././.")          == "." );    assert( d.NormalizePath("../../../..")      == "../../../.." );    assert( d.NormalizePath("dir//dir//")       == "dir/dir" );    assert( d.NormalizePath("///dir//")         == "/dir" );    assert( d.NormalizePath("dir\\file")        == "dir\\file" );#elif defined(NCBI_OS_MAC)    // NOT implemented!#endif}/////////////////////////////////// File name maching test//static void s_TEST_MatchesMask(void){    CDirEntry d;        assert( d.MatchesMask(""        ,""));    assert( d.MatchesMask("file"    ,"*"));    assert(!d.MatchesMask("file"    ,"*.*"));    assert( d.MatchesMask("file.cpp","*.cpp"));    assert( d.MatchesMask("file.cpp","*.c*"));    assert( d.MatchesMask("file"    ,"file*"));    assert( d.MatchesMask("file"    ,"f*"));    assert( d.MatchesMask("file"    ,"f*le"));    assert( d.MatchesMask("file"    ,"f**l*e"));    assert(!d.MatchesMask("file"    ,"???"));    assert( d.MatchesMask("file"    ,"????"));    assert(!d.MatchesMask("file"    ,"?????"));    assert( d.MatchesMask("file"    ,"?i??"));    assert(!d.MatchesMask("file"    ,"?l??"));    assert(!d.MatchesMask("file"    ,"?i?"));    assert( d.MatchesMask("file"    ,"?*?"));    assert( d.MatchesMask("file"    ,"?***?"));    assert( d.MatchesMask("file"    ,"?*"));    assert( d.MatchesMask("file"    ,"*?"));    assert( d.MatchesMask("file"    ,"********?"));}/////////////////////////////////// Work with files//static void s_TEST_File(void){    {{        // Create test file         FILE* file = fopen("file_1", "w+");        assert( file );        fputs("test data", file);        fclose(file);            CFile f("file_1");        // Get file size        assert( f.GetLength() == 9);        CFile("file_2").Remove();        assert( CFile("file_2").GetLength() == -1);        // Check the file exists        assert( f.Exists() );        assert( !CFile("file_2").Exists() );        // Rename the file        assert( f.Rename("file_2") );        // Status        CDirEntry::EType file_type;        file_type = f.GetType();         CDirEntry::TMode user, group, other;        user = group = other = 0;        assert ( f.GetMode(&user, &group, &other) );        cout << "File type : " << file_type << endl;        cout << "File mode : "             << ((user  & CDirEntry::fRead)    ? "r" : "-")             << ((user  & CDirEntry::fWrite)   ? "w" : "-")             << ((user  & CDirEntry::fExecute) ? "x" : "-")             << ((group & CDirEntry::fRead)    ? "r" : "-")             << ((group & CDirEntry::fWrite)   ? "w" : "-")             << ((group & CDirEntry::fExecute) ? "x" : "-")             << ((other & CDirEntry::fRead)    ? "r" : "-")             << ((other & CDirEntry::fWrite)   ? "w" : "-")             << ((other & CDirEntry::fExecute) ? "x" : "-")             << endl;        // Get/set file modification time        CTime::SetFormat("M/D/Y h:m:s Z");        CTime mtime, ctime, atime;        assert( f.GetTime(&mtime, &ctime , &atime) );        cout << "File creation time     : " << ctime.AsString() << endl;        cout << "File modification time : " << mtime.AsString() << endl;        cout << "File last access time  : " << atime.AsString() << endl;        assert( f.GetTime(&mtime, 0 , &atime) );        CTime mtime_new(mtime), atime_new(atime);        mtime_new.AddDay(-2);        atime_new.AddDay(-1);        assert( f.SetTime(&mtime_new, &atime_new) );        assert( f.GetTime(&mtime, &atime) );        cout << "File modification time : " << mtime.AsString() << endl;        cout << "File last access time  : " << atime.AsString() << endl;        assert( mtime == mtime_new );        // Remove the file        assert( f.Remove() );        // Check the file exists        assert( !CFile("file_1").Exists() );        assert( !CFile("file_2").Exists() );    }}        {{        // Create temporary file        const string kTestData = "testdata";        fstream* stream = CFile::CreateTmpFile();        assert( stream );        assert( (*stream).good() );        *stream << kTestData;        assert( (*stream).good() );        (*stream).flush();        (*stream).seekg(0);        string str;        (*stream) >> str;        assert( (*stream).eof() );        assert( str == kTestData );        delete stream;        stream = CFile::CreateTmpFile("test.tmp");        assert( stream );        assert( (*stream).good() );        *stream << kTestData;        assert( (*stream).good() );        (*stream).flush();        (*stream).seekg(0);        (*stream) >> str;        assert( (*stream).eof() );        assert( str == kTestData );        delete stream;        // Get temporary file name        string fn;        fn = CFile::GetTmpName();        assert( !fn.empty() );        assert( !CFile(fn).Exists() );        fn = CFile::GetTmpNameEx(".", "tmp_");        assert( !fn.empty() );        assert( !CFile(fn).Exists() );#  if defined(NCBI_OS_MSWIN)  ||  defined(NCBI_OS_UNIX)        fn = CFile::GetTmpName(CFile::eTmpFileCreate);        assert( !fn.empty() );        assert( CFile(fn).Exists() );        CFile(fn).Remove();        fn = CFile::GetTmpNameEx(".", "tmp_", CFile::eTmpFileCreate);        assert( !fn.empty() );        assert( CFile(fn).Exists() );        CFile(fn).Remove();#  endif    }}}/////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -