/* This file is part of c++script. c++script is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. c++script is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with c++script. If not, see . */ /* Various tests of the c++script library */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "c++script.h" #include "getnow.h" using namespace std; #include #include static void test_fct (string_view type, string_view s, int nbrepeat) { auto start = getnow(); if (type == "basename"){ if (nbrepeat > 1){ for (int i=0; i {}\n",s,cpps::basename(s)); } }else if (type == "dirname"){ if (nbrepeat > 1){ for (int i=0; i {}\n",s,cpps::dirname(s)); } }else if (type == "select"){ // The lambda function receives the first argument sent to the select // function. In case of the case "test1", this has dubious value // since val == "test1" // But the various cases are regex, so in the last case, t.*5 match // anything starting with t and ending with 5, so val is indeed // useful here. cpps::select (s ,"test1",[](auto val){ cout << format ("select match test1 val = {}\n",val); } ,"test2",[](auto val){ cout << format ("select match test2 val = {}\n",val); } ,"test3",[](auto val){ cout << format ("select match test3 val = {}\n",val); } ,"test4",[](auto val){ cout << format ("select match test4 val = {}\n",val); } ,"t.*5",[](auto val){ cout << format ("select match t.*5 val = {}\n",val); } ); }else if (type == "FILENAME"){ cpps::FILENAME f(s); cout << format ("basename={} dirname={} absdir={} abspath={}\n" ,f.basename(),f.dirname(),f.absdir(),f.abspath()); }else if (type == "FILENAME.cat"){ cpps::FILENAME f(s); for (int i=0; i {}\n",s,cpps::FILENAME(s).exist()); }else if (type == "FILENAME.is_empty"){ cout << format ("FILENAME.is_empty {} -> {}\n",s,cpps::FILENAME(s).is_empty()); }else if (type == "FILENAME.mayexec"){ cout << format ("FILENAME.mayexec {} -> {}\n",s,cpps::FILENAME(s).mayexec()); }else if (type == "FILENAME.unlink"){ cpps::FILENAME file(s); cout << format ("FILENAME.exist {} -> {}\n",s,file.exist()); cout << format ("FILENAME.unlink {} -> {}\n",s,file.unlink()); cout << format ("FILENAME.exist {} -> {}\n",s,file.exist()); }else if (type == "trf_towords"){ cpps::FILENAME file(s); cout << format ("Split file {} into words\n",s); for (auto &&tb:file.cat()|cpps::trf_towords()){ for (auto &w:tb){ cout << format (" <{}>",w); } cout << "\n"; } struct LINES{ string line1,line2; }; vector tblines={ {"1-hello how are you","1-hello1 how1 are1 you1"}, {"2-hello how are you","2-hello1 how1 are1 you1"}, {"3-hello how are you","3-hello1 how1 are1 you1"} }; cout << format ("Split tblines into words using projection &LINES::line1\n",s); for (auto &&tb:tblines|cpps::trf_towords(' ',&LINES::line1)){ for (auto &w:tb){ cout << format (" <{}>",w); } cout << "\n"; } cout << format ("Split tblines into words using projection &LINES::line2\n",s); for (auto &&tb:tblines|cpps::trf_towords(' ',&LINES::line2)){ for (auto &w:tb){ cout << format (" <{}>",w); } cout << "\n"; } }else if (type == "DIRNAME"){ cpps::DIRNAME f(s); cout << format ("dirname={} parent={} basename={} dirname={} absdir={} abspath={}\n" ,s,f.parent(),f.basename(),f.dirname(),f.absdir(),f.abspath()); }else if (type == "DIRNAME.lsold"){ cpps::DIRNAME f(s); for (auto i=f.ls().begin(); i != f.ls().end(); ++i){ cout << format ("DIRNAME.ls name={} inode={} entry_num={}\n",i.name,i.inode,i.entry_num); } }else if (type == "DIRNAME.lsrange"){ cpps::DIRNAME f(s); for (auto &i:f.ls()){ cout << format ("DIRNAME.ls name={} inode={} entry_num={}\n",i.name,i.inode,i.entry_num); } }else if (type == "DIRNAME.lsdrop"){ cpps::DIRNAME f(s); for (auto &i:f.ls()|views::drop(5)){ cout << format ("DIRNAME.ls name={} inode={} entry_num={}\n",i.name,i.entry_num,i.entry_num); } }else if (type == "DIRNAME.lstake"){ cpps::DIRNAME f(s); for (auto &i:f.ls()|views::take(5)){ cout << format ("DIRNAME.ls name={} inode={} entry_num={}\n",i.name,i.inode,i.entry_num); } }else if (type == "DIRNAME.ls_l"){ cpps::DIRNAME f(s); for (auto i:f.ls_l()|views::take(10)){ cout << format ("DIRNAME.ls_l name={} inode={} size={}\n",i.name,i.inode,i.size); } }else if (type == "DIRNAME.ls_pat"){ cpps::DIRNAME f("/tmp"); for (auto i:f.ls(s)|views::take(10)){ cout << format ("DIRNAME.ls({}) name={}\n",s,i.name); } }else if (type == "DIRNAME.ls_l_pat"){ cpps::DIRNAME f("/tmp"); for (auto i:f.ls_l(s)|views::take(10)){ cout << format ("DIRNAME.ls_l({}) name={} inode={} size={}\n",s,i.name,i.inode,i.size); } }else if (type == "DIRNAME.ls_l2"){ cpps::DIRNAME f(s); for (auto i:f.ls()|views::take(10)|f.stat()){ cout << format ("DIRNAME.ls_l name={} inode={} size={} mode={} times={}.{} {}.{} {}.{} dev={} {}\n" ,i.name,i.inode,i.size,i.mode ,i.created.tv_sec,i.created.tv_nsec ,i.modified.tv_sec,i.modified.tv_nsec ,i.accessed.tv_sec,i.accessed.tv_nsec ,i.dev,i.rdev); } }else if (type == "ps"){ for (int i=0; i tb={{"hello","hello1","hello11"},{"how","how2","how22"},{"are","are3","are33"},{"you","you4","you44"}}; struct stuff_test{ string name; string stuff::*pt; }; vector tbtest={{"name1",&stuff::name1},{"name2",&stuff::name2},{"name3",&stuff::name3}}; for (auto &t:tbtest){ cout << format ("match {} {} ->\n",t.name,s); for (auto i:tb|cpps::match(s,std::regex_constants::basic,t.pt)){ cout << format ("\tmatch {} {} {}\n",i.name1,i.name2,i.name3); } } for (auto &t:tbtest){ cout << format ("nomatch {} {} ->\n",t.name,s); for (auto i:tb|cpps::nomatch(s,std::regex_constants::basic,t.pt)){ cout << format ("\tnomatch {} {} {}\n",i.name1,i.name2,i.name3); } } for (auto &t:tbtest){ cout << format ("contain {} {} ->\n",t.name,s); for (auto i:tb|cpps::contain(s,std::regex_constants::basic,t.pt)){ cout << format ("\tcontain {} {} {}\n",i.name1,i.name2,i.name3); } } for (auto &t:tbtest){ cout << format ("not_contain {} {} ->\n",t.name,s); for (auto i:tb|cpps::not_contain(s,std::regex_constants::basic,t.pt)){ cout << format ("\tnot_contain {} {} {}\n",i.name1,i.name2,i.name3); } } }else if (type == "match"){ cpps::DIRNAME f("/tmp"); cout << format ("match {}\n",s); for (auto i:f.ls()|cpps::match(s)){ cout << format ("match name={} inode={} entry_num={}\n",i.name,i.inode,i.entry_num); } }else if (type == "nomatch"){ cpps::DIRNAME f("/tmp"); cout << format ("nomatch {}\n",s); for (auto i:f.ls()|cpps::nomatch(s)){ cout << format ("nomatch name={} inode={} entry_num={}\n",i.name,i.inode,i.entry_num); } }else if (type == "contain"){ cpps::DIRNAME f("/tmp"); cout << format ("contain {}\n",s); for (auto i:f.ls()|cpps::contain(s)){ cout << format ("contain name={} inode={} entry_num={}\n",i.name,i.inode,i.entry_num); } }else if (type == "not_contain"){ cpps::DIRNAME f("/tmp"); cout << format ("not_contain {}\n",s); for (auto i:f.ls()|cpps::not_contain(s)){ cout << format ("not_contain name={} inode={} entry_num={}\n",i.name,i.inode,i.entry_num); } }else if (type == "string"){ string v(s); for (auto c:v|views::split('/')){ cout << "string "; for (auto cc:c) cout << cc; cout << "\n"; } }else{ cout << "Unknown test\n"; } auto end = getnow(); if (nbrepeat > 1) showtime (type,start,end); } int main (int argc, char *argv[]) { int nbrepeat = 1; if (argc > 1){ int nbrepeat = 1; for (int i=1; i