t_byte_filer.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_byte_filer                                                   *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include <basis/byte_array.h>
00016 #include <basis/chaos.h>
00017 #include <basis/function.h>
00018 #include <basis/guards.h>
00019 #include <basis/istring.h>
00020 #include <basis/portable.h>
00021 #include <basis/string_array.h>
00022 #include <opsystem/application_shell.h>
00023 #include <opsystem/byte_filer.h>
00024 #include <loggers/console_logger.h>
00025 #include <loggers/file_logger.h>
00026 #include <opsystem/directory.h>
00027 #include <opsystem/filename.h>
00028 #include <data_struct/static_memory_gremlin.h>
00029 
00030 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00031 
00032 class test_byte_filer : public application_shell
00033 {
00034 public:
00035   test_byte_filer() : application_shell(class_name()) {}
00036   IMPLEMENT_CLASS_NAME("test_byte_filer");
00037   int run_simple_test();
00038   int run_file_scan();
00039   virtual int execute();
00040 };
00041 
00042 const istring TEST_FILE_BASE = "garbage.txt";
00043 
00044 const istring TEST_FILE = portable::env_string("TMP") + "/" + TEST_FILE_BASE;
00045 
00046 int test_byte_filer::run_simple_test()
00047 {
00048   FUNCDEF("run_simple_test");
00049 #ifdef DEBUG
00050   LOG("ahoy, beginning file test...");
00051 #endif
00052   chaos randomizer;
00053 
00054 //hmmm: move to t_filename.
00055   // test filename's exist operation.
00056   byte_filer garbage(TEST_FILE.s(), "wb");
00057   garbage.write("oy.\n");
00058   garbage.close();
00059   filename test1(TEST_FILE);
00060   if (!test1.exists())
00061     deadly_error(class_name(), "exists test", "file didn't exist when should!");
00062   filename test2("c:\this_file_shouldNt_exist_ever.txt");
00063   if (test2.exists())
00064     deadly_error(class_name(), "exists test", "file existed when shouldn't!");
00065   // test again to make sure it didn't create it.
00066   if (test2.exists())
00067     deadly_error(class_name(), "exists test", "file existed when shouldn't!");
00068   test1.unlink();
00069 
00070   int block_size = randomizer.inclusive(3000, 30000);
00071 #ifdef DEBUG
00072   LOG(isprintf("block size=%d", block_size));
00073 #endif
00074   byte *original_block = new byte[block_size];
00075   for (int i = 0; i < block_size; i++)
00076     original_block[i] = byte(randomizer.inclusive(32, 126));
00077   unsigned int original_checksum
00078       = utility::bizarre_checksum((byte *)original_block, block_size);
00079   if (original_checksum) {} // compiler quieting.
00080 #ifdef DEBUG
00081   LOG(isprintf("random block checksum=%d", original_checksum));
00082 #endif
00083   {
00084     byte_array to_stuff_in_file(block_size, original_block);
00085     delete [] original_block;
00086     byte_filer fred(TEST_FILE, "w+");
00087     fred.write(to_stuff_in_file);
00088   }
00089 #ifdef DEBUG
00090   LOG(istring("about to compare file to checksum"));
00091 #endif
00092   {
00093     byte *temp_array = new byte[21309];
00094     byte_array to_fake_stuff(21309, temp_array);
00095     delete [] temp_array;
00096     byte_filer fred(TEST_FILE, "r");
00097 #ifdef DEBUG
00098     LOG(istring("about to try writing to file"));
00099 #endif
00100     int should_be_failure = fred.write(to_fake_stuff);
00101     if (should_be_failure != 0)
00102       deadly_error(class_name(), "write on read only",
00103           istring(istring::SPRINTF, "write supposedly succeeded (len = %d)!",
00104               should_be_failure));
00107 
00108 }//temp
00109 
00110 //hmmm: uhhh, why is the below all disabled?
00111 
00112 
00113 /*
00114 #ifdef DEBUG
00115     LOG(isprintf("about to try reading from file %d bytes", fredsize));
00116 #endif
00117     byte_array file_contents = fred.next();
00118     unsigned int check_2
00119       = utility::bizarre_checksum((byte *)file_contents.access(), file_contents.length());
00120     if (check_2 != original_checksum)
00121       deadly_error(class_name(), "file comparison", "next() read different contents!");
00122   }
00123 
00124 #define FACTOR 1354
00125 
00126   {
00127     int numpacs = number_of_packets(block_size, FACTOR);
00128     file fred(TEST_FILE, file::READ_ONLY);
00129     fred.chunk_factor(FACTOR);
00130     int whole_size = 0;
00131     for (int i = 0; i < numpacs; i++) {
00132       byte_array blob_i = fred[i];
00133       whole_size += blob_i.length();
00134     }
00135     if (whole_size != fred.size())
00136       deadly_error(class_name(), "chunking comparison", "sizes are different");
00137   }
00138 
00139 // test writing out a copy and comparing them... there's no == on files!
00140 
00141   remove(TEST_FILE);
00142 */
00143 
00144   // it seems everything worked during our tests.
00145   return 0;
00146 }
00147 
00148 int test_byte_filer::run_file_scan()
00149 {
00150   FUNCDEF("run_file_scan");
00151   chaos randomizer;
00152 
00153   string_array files(__argc, (const char **)__argv);
00154   files.zap(0, 0);  // toss the first element since that's our app filename.
00155 
00156   if (!files.length()) {
00157     // pretend they gave us the list of files in the TMP directory.  some of
00158     // these might fail if they're locked up.
00159 //    istring tmpdir = portable::env_string("TMP");
00160     istring tmpdir = portable::current_directory();
00161     directory dir(tmpdir);
00162     for (int i = 0; i < dir.files().length(); i++) {
00163       // skip text files since we use those right here.
00164       if ( (dir.files()[i].ends(".txt")) || (dir.files()[i].ends(".txt")) )
00165         continue;
00166       istring chewed_string = tmpdir + "/" + dir.files()[i];
00167       files += chewed_string;
00168     }
00169 LOG(istring("added files since no cmd args: ") + files.text_form());
00170   }
00171 
00172   byte_array data_found;
00173   for (int i = 0; i < files.length(); i++) {
00174     istring curr = files[i];
00175     LOG(isprintf("file %d: ", i) + curr);
00176     byte_filer test(curr, "rb");
00177     if (!test.good()) {
00178       LOG(istring("good check: ") + curr + " cannot be opened.  is this bad?");
00179       continue;
00180     }
00181 
00182     // check that we get the expected position report from scooting to the
00183     // end of a file.
00184     test.seek(0, byte_filer::FROM_END);
00185     if (test.tell() != test.length())
00186       deadly_error(class_name(), "seek check",
00187           isprintf("seek to end, posn at %d bytes but should be at %d",
00188               test.tell(), test.length()));
00189     test.seek(0, byte_filer::FROM_START);
00190 
00191     size_t len = test.length();
00192 //log(isprintf("file len is %.0f", double(len)));
00193     size_t posn = 0;
00194     while ( (posn < len) && !test.eof() ) {
00195       size_t readlen = randomizer.inclusive(1, 256 * KILOBYTE);
00196 //log(isprintf("read %u bytes, posn now %d bytes", readlen, posn));
00197       int bytes_read = int(test.read(data_found, int(readlen)));
00198       if (bytes_read < 0) {
00199         deadly_error(class_name(), "reading", curr + " failed to be read.");
00200       } else {
00201         posn += bytes_read;
00202       }
00203     }
00204     if (!test.eof())
00205       deadly_error(class_name(), "eof check", curr + " not at eof.");
00206     if (posn != len)
00207       deadly_error(class_name(), "eof check", curr + " is at wrong position: "
00208           + isprintf("want %.0f, got %.0f", double(len), double(posn)));
00209     log(istring("successfully read ") + curr);
00210   }
00211 
00212   return 0;
00213 }
00214 
00215 int test_byte_filer::execute()
00216 {
00217   FUNCDEF("execute");
00218   SET_DEFAULT_COMBO_LOGGER;
00219   int ret = run_simple_test();
00220   if (ret) return ret;  // failed.
00221   ret = run_file_scan();
00222   if (ret) return ret;  // failed here.
00223 
00224   guards::alert_message("byte_filer:: works for those functions tested.\n");
00225   return 0;
00226 }
00227 
00228 HOOPLE_MAIN(test_byte_filer, )
00229 

Generated on Fri Nov 28 04:29:39 2008 for HOOPLE Libraries by  doxygen 1.5.1