jbm: it compiles... but bombs in unit tests
authorjbm@humility
Mon Sep 28 23:39:08 2009 -0700 (2009-09-28)
changeset 821a6c17f4e3e
parent 7 2e44bc2caefe
child 9 629f61203db1
jbm: it compiles... but bombs in unit tests
include/Base.h
include/File.h
include/cf_fixup.h
src/Base.cpp
src/File.cpp
src/Hash.cpp
src/Index.cpp
src/Ottoman.cpp
src/VersionDictionary.cpp
test/TestUtils.cpp
test/VersionDictionary_test.cpp
     1.1 --- a/include/Base.h	Sat Sep 26 12:45:03 2009 -0700
     1.2 +++ b/include/Base.h	Mon Sep 28 23:39:08 2009 -0700
     1.3 @@ -12,9 +12,20 @@
     1.4  #include <stdlib.h>
     1.5  #include <stdint.h>
     1.6  #include <string.h>
     1.7 +
     1.8 +#if OSX
     1.9 +/* OS X specific bits */
    1.10  #include <libkern/OSByteOrder.h>
    1.11  #include <CoreFoundation/CFByteOrder.h>
    1.12  
    1.13 +#else
    1.14 +/* OS X fixup kludge bits */
    1.15 +
    1.16 +#include <cf_fixup.h>
    1.17 +#include <stdint.h>
    1.18 +
    1.19 +#endif
    1.20 +
    1.21  namespace Mooseyard {
    1.22      
    1.23      /** A 32-bit file position/offset. Supports files of up to 4GB. */
    1.24 @@ -110,4 +121,4 @@
    1.25      
    1.26  }
    1.27  
    1.28 -#endif _MOOSEYARD_BASE_
    1.29 +#endif /* _MOOSEYARD_BASE_ */
     2.1 --- a/include/File.h	Sat Sep 26 12:45:03 2009 -0700
     2.2 +++ b/include/File.h	Mon Sep 28 23:39:08 2009 -0700
     2.3 @@ -31,7 +31,7 @@
     2.4      
     2.5      
     2.6      File (const char *filename, bool writeable =false, bool create =false)  throw(Error);
     2.7 -    File (const char *filename, int oflag)  throw(Error);
     2.8 +    File (const char *filename, int oflag, bool locked)  throw(Error);
     2.9      ~File();
    2.10      
    2.11      off_t length() const  throw(Error);
    2.12 @@ -89,9 +89,11 @@
    2.13      
    2.14  private:
    2.15      static int _open (const char *filename, int posixMode)  throw(Error);
    2.16 +    static int _check (int result)  throw(Error);
    2.17 +#ifdef OSX /* On linux, these are the same type signature to gcc */
    2.18      static ssize_t _check (ssize_t result)  throw(Error)    {_check((int)result); return result;}
    2.19      static off_t _check (off_t result)  throw(Error)    {_check((int)result); return result;}
    2.20 -    static int _check (int result)  throw(Error);
    2.21 +#endif
    2.22      static void _checkRead (ssize_t result, size_t expectedSize)  throw(Error);
    2.23      bool _lock (bool block);
    2.24      void _unlock();
    2.25 @@ -106,4 +108,4 @@
    2.26  
    2.27  }
    2.28  
    2.29 -#endif _MOOSEYARD_FILE_
    2.30 +#endif /* _MOOSEYARD_FILE_ */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/include/cf_fixup.h	Mon Sep 28 23:39:08 2009 -0700
     3.3 @@ -0,0 +1,9 @@
     3.4 +typedef double CFSwappedFloat64;
     3.5 +
     3.6 +static inline CFSwappedFloat64 CFConvertDoubleHostToSwapped(double d) { return d; };
     3.7 +static inline double CFConvertDoubleSwappedToHost(CFSwappedFloat64 d) { return d; };
     3.8 +
     3.9 +#define OSSwapHostToLittleInt32(x) (x)
    3.10 +#define OSSwapHostToLittleInt16(x) (x)
    3.11 +#define OSSwapLittleToHostInt32(x) (x)
    3.12 +#define OSSwapLittleToHostInt16(x) (x)
     4.1 --- a/src/Base.cpp	Sat Sep 26 12:45:03 2009 -0700
     4.2 +++ b/src/Base.cpp	Mon Sep 28 23:39:08 2009 -0700
     4.3 @@ -11,7 +11,10 @@
     4.4  #include "File.h"
     4.5  #include <assert.h>
     4.6  #include <errno.h>
     4.7 +
     4.8 +#if USE_REMOVED_UUID
     4.9  #include <CoreFoundation/CFUUID.h>
    4.10 +#endif
    4.11  
    4.12  namespace Mooseyard {
    4.13      
    4.14 @@ -39,7 +42,7 @@
    4.15  #pragma mark -
    4.16  #pragma mark UUID:
    4.17    
    4.18 -#if 0
    4.19 +#if USE_REMOVED_UUID
    4.20      /** Typical 128-bit universally-unique identifier. */
    4.21      class UUID {
    4.22      public:
     5.1 --- a/src/File.cpp	Sat Sep 26 12:45:03 2009 -0700
     5.2 +++ b/src/File.cpp	Mon Sep 28 23:39:08 2009 -0700
     5.3 @@ -10,6 +10,7 @@
     5.4  #include "File.h"
     5.5  #include "MemoryMap.h"
     5.6  
     5.7 +#include <sys/file.h>
     5.8  #include <assert.h>
     5.9  #include <errno.h>
    5.10  #include <fcntl.h>
    5.11 @@ -26,11 +27,19 @@
    5.12           _locked(0)
    5.13      { }
    5.14  
    5.15 -    File::File (const char *filename, int oflag)  throw(Error)
    5.16 +    File::File (const char *filename, int oflag, bool locked)  throw(Error)
    5.17 +#if BSD
    5.18 +        :_fd(_check( ::open(filename, oflag | (locked ? O_EXLOCK : 0), 0644) )),
    5.19 +#else
    5.20          :_fd(_check( ::open(filename, oflag, 0644) )),
    5.21 +#endif
    5.22           _memoryMap(NULL),
    5.23 -         _locked( (oflag | O_EXLOCK) ?1 :0)
    5.24 -    { }
    5.25 +         _locked( locked ? 1 : 0)
    5.26 +    {
    5.27 +#if !BSDISH
    5.28 +      _check( ::flock(_fd, LOCK_EX) );
    5.29 +#endif
    5.30 +    }
    5.31  
    5.32      File::~File() {
    5.33          delete _memoryMap;
    5.34 @@ -102,7 +111,11 @@
    5.35      }
    5.36      
    5.37      void File::flushDisk()  throw(Error) {
    5.38 +#if OSX
    5.39          _check( ::fcntl(_fd,F_FULLFSYNC) );
    5.40 +#else
    5.41 +        _check( fsync(_fd) );
    5.42 +#endif
    5.43      }
    5.44      
    5.45      bool File::hasPath (const char *path) const  throw(Error) {
     6.1 --- a/src/Hash.cpp	Sat Sep 26 12:45:03 2009 -0700
     6.2 +++ b/src/Hash.cpp	Mon Sep 28 23:39:08 2009 -0700
     6.3 @@ -9,6 +9,7 @@
     6.4  
     6.5  #include "Hash.h"
     6.6  #include "Dictionary.h"
     6.7 +#include <stdio.h>
     6.8  #include <algorithm>
     6.9  
    6.10  namespace Mooseyard {
    6.11 @@ -110,7 +111,7 @@
    6.12          for (int index=0; index<_tableSize; index++)
    6.13              if (_table[index].hasValue()) {
    6.14                  const Key &key = _table[index].key();
    6.15 -                printf("%3i: %*s -> %p\n", 
    6.16 +                ::printf("%3i: %*s -> %p\n", 
    6.17                         index, (int)key.length,(const char*)key.bytes, _table[index].value());
    6.18              }
    6.19      }
     7.1 --- a/src/Index.cpp	Sat Sep 26 12:45:03 2009 -0700
     7.2 +++ b/src/Index.cpp	Mon Sep 28 23:39:08 2009 -0700
     7.3 @@ -16,6 +16,12 @@
     7.4  #include <algorithm>
     7.5  #include <math.h>
     7.6  
     7.7 +#define __STDC_LIMIT_MACROS /* I <3 you, C99 */
     7.8 +# define UINT32_MAX             (4294967295U)
     7.9 + 
    7.10 +#include <stdint.h>
    7.11 +
    7.12 +
    7.13  namespace Mooseyard {
    7.14          
    7.15      // Quadratic probing results in fewer bucket tests on lookup, but makes the hash tables
     8.1 --- a/src/Ottoman.cpp	Sat Sep 26 12:45:03 2009 -0700
     8.2 +++ b/src/Ottoman.cpp	Mon Sep 28 23:39:08 2009 -0700
     8.3 @@ -42,7 +42,6 @@
     8.4      
     8.5      Ottoman::Ottoman()
     8.6      :_writeable(true),
     8.7 -     _file(NULL),
     8.8       _filename(NULL),
     8.9       _lastVersion(NULL),
    8.10       _current( new OverlayDictionary(&Dictionary::kEmpty) )
    8.11 @@ -97,9 +96,6 @@
    8.12              delete _file;
    8.13              _file = curFile;
    8.14          }
    8.15 -        
    8.16 -        if (changed)
    8.17 -            versionsChanged();
    8.18          return changed;
    8.19      }
    8.20      
    8.21 @@ -200,12 +196,11 @@
    8.22          if (!lastVersion)
    8.23              lastVersion = new VersionDictionary(dstFile);
    8.24          VersionDictionary *saved = lastVersion->_appendAndOpen(_current->overlay(), 
    8.25 -                                                               dstFile,
    8.26 -                                                               _current->baseReplaced());
    8.27 +                                                            dstFile,
    8.28 +                                                            _current->baseReplaced());
    8.29          // (don't delete _lastVersion: saved->_previousVersion now points to it.)
    8.30          _lastVersion = saved;
    8.31          _current->revertTo(_lastVersion);
    8.32 -        versionsChanged();
    8.33      }
    8.34      
    8.35      bool Ottoman::save() {
    8.36 @@ -221,10 +216,10 @@
    8.37      }
    8.38      
    8.39      File* Ottoman::_writeTo (const char *dstFileName, bool overwriteAllowed) {
    8.40 -        int mode = O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK;
    8.41 +        int mode = O_RDWR | O_CREAT | O_TRUNC;
    8.42          if (!overwriteAllowed)
    8.43              mode |= O_EXCL;
    8.44 -        File *dstFile = new File(dstFileName, mode);
    8.45 +        File *dstFile = new File(dstFileName, mode, true);
    8.46          try {
    8.47              dstFile->write(DictionaryFileHeader());
    8.48              _append(dstFile);
    8.49 @@ -240,7 +235,6 @@
    8.50          File *dstFile = _writeTo(dstFileName, overwriteAllowed);
    8.51          free(_filename);
    8.52          _filename = strdup(dstFileName);
    8.53 -        delete _file;
    8.54          _file = dstFile;
    8.55      }
    8.56  
    8.57 @@ -248,8 +242,8 @@
    8.58          if (!_file)
    8.59              return false;
    8.60          char tempFileName[1024];
    8.61 -        strlcpy(tempFileName, _filename, sizeof(tempFileName)-1);
    8.62 -        strlcat(tempFileName, "~", sizeof(tempFileName));
    8.63 +        ::strncpy(tempFileName, _filename, sizeof(tempFileName)-1);
    8.64 +        ::strncat(tempFileName, "~", sizeof(tempFileName));
    8.65          File *tempFile;
    8.66          {
    8.67              // Check for conflict in existing file:
     9.1 --- a/src/VersionDictionary.cpp	Sat Sep 26 12:45:03 2009 -0700
     9.2 +++ b/src/VersionDictionary.cpp	Mon Sep 28 23:39:08 2009 -0700
     9.3 @@ -15,6 +15,10 @@
     9.4  #include <assert.h>
     9.5  #include <errno.h>
     9.6  #include <algorithm>
     9.7 +#include <time.h>
     9.8 +
     9.9 +# define UINT32_MAX             (4294967295U)
    9.10 +
    9.11  
    9.12  namespace Mooseyard {
    9.13              
    10.1 --- a/test/TestUtils.cpp	Sat Sep 26 12:45:03 2009 -0700
    10.2 +++ b/test/TestUtils.cpp	Mon Sep 28 23:39:08 2009 -0700
    10.3 @@ -11,6 +11,7 @@
    10.4  #include "File.h"
    10.5  #include <time.h>
    10.6  #include <sys/time.h>
    10.7 +#include <stdio.h>
    10.8  
    10.9  namespace Mooseyard {
   10.10  
   10.11 @@ -24,7 +25,9 @@
   10.12      
   10.13      void shuffle(int a[], int n, unsigned seed) {
   10.14          if (seed==0) {
   10.15 +#ifdef BSD
   10.16              srandomdev();
   10.17 +#endif
   10.18              seed = random();
   10.19          }
   10.20          fprintf(stderr,"shuffle(n=%i, seed=%u)\n", n,seed);
   10.21 @@ -46,15 +49,18 @@
   10.22              sNWords = 0;
   10.23              
   10.24              FILE *in = fopen("/usr/share/dict/words", "r");
   10.25 -            const char *word;
   10.26 +            char word[4096];
   10.27              size_t wordLen;
   10.28 -            while (NULL != (word=fgetln(in,&wordLen))) {
   10.29 -                if (word[wordLen-1]=='\n')
   10.30 -                    wordLen--;
   10.31 -                char *str = new char[wordLen+1];
   10.32 -                memcpy(str, word, wordLen);
   10.33 -                str[wordLen] = 0;
   10.34 -                sWords[sNWords] = str;
   10.35 +            while (NULL != ::fgets(word, 4096, in)) {
   10.36 +                wordLen = ::strlen(word);
   10.37 +                if (word[wordLen-1]=='\n') {
   10.38 +		  word[wordLen-1] = '\0';
   10.39 +		  wordLen--;
   10.40 +		}
   10.41 +
   10.42 +                if (wordLen == 0) continue;
   10.43 +
   10.44 +                sWords[sNWords] = strdup(word); 
   10.45                  //if( sNWords % 10000 == 0)
   10.46                  //    printf("'%s' ... ", sWords[sNWords]->string());
   10.47                  sNWords++;
   10.48 @@ -93,7 +99,9 @@
   10.49  using namespace Mooseyard;
   10.50  
   10.51  int main(int argc, char **argv) {
   10.52 +#if BSD
   10.53      srandomdev();
   10.54 +#endif
   10.55      try {
   10.56          ::testing::InitGoogleTest(&argc, argv);
   10.57          return RUN_ALL_TESTS();
    11.1 --- a/test/VersionDictionary_test.cpp	Sat Sep 26 12:45:03 2009 -0700
    11.2 +++ b/test/VersionDictionary_test.cpp	Mon Sep 28 23:39:08 2009 -0700
    11.3 @@ -46,16 +46,16 @@
    11.4  
    11.5  TEST(File,HasPath) {
    11.6      {
    11.7 -        File f("/tmp/jubba", O_RDWR | O_CREAT | O_TRUNC);
    11.8 +        File f("/tmp/jubba", O_RDWR | O_CREAT | O_TRUNC, true);
    11.9          f.write("howdy");
   11.10      }
   11.11      {
   11.12 -        File f("/tmp/jubba", O_RDWR);
   11.13 +        File f("/tmp/jubba", O_RDWR, true);
   11.14          EXPECT_TRUE(f.hasPath("/tmp/jubba"));
   11.15          File::unlink("/tmp/jubba");
   11.16          EXPECT_FALSE(f.hasPath("/tmp/jubba"));
   11.17 -        
   11.18 -        File f2("/tmp/jubba", O_RDWR | O_CREAT | O_TRUNC);
   11.19 +
   11.20 +        File f2("/tmp/jubba", O_RDWR | O_CREAT | O_TRUNC, true);
   11.21          f2.write("howdy");
   11.22          f2.flush();
   11.23  
   11.24 @@ -108,7 +108,7 @@
   11.25      time_t startTime = ::time(NULL);
   11.26      time_t createTime;
   11.27      {
   11.28 -        File file("/tmp/hashfiletest", O_RDWR | O_CREAT | O_TRUNC);
   11.29 +        File file("/tmp/hashfiletest", O_RDWR | O_CREAT | O_TRUNC, true);
   11.30          VersionDictionary *hf;
   11.31          {
   11.32              Timer t("Creating & writing VersionDictionary", nWords);
   11.33 @@ -152,7 +152,7 @@
   11.34      }
   11.35      {
   11.36          printf("Opening OverlayVersionDictionary...\n");
   11.37 -        File file("/tmp/hashfiletest", O_RDWR);
   11.38 +        File file("/tmp/hashfiletest", O_RDWR, true);
   11.39          OverlayVersionDictionary hf(&file);
   11.40          EXPECT_EQ( nWords ,  hf.count() );
   11.41          EXPECT_TRUE(hf.get("abatement").equals("abatement"));
   11.42 @@ -225,7 +225,7 @@
   11.43          File oldFile("/tmp/hashfiletest");
   11.44          OverlayVersionDictionary oldhf(&oldFile);
   11.45          
   11.46 -        File newFile("/tmp/hashfiletest2", O_RDWR | O_CREAT | O_TRUNC);
   11.47 +        File newFile("/tmp/hashfiletest2", O_RDWR | O_CREAT | O_TRUNC, true);
   11.48          newFile.write("Ha5h", 4);     // VersionDictionary won't write to an empty file
   11.49          oldhf.saveAs(&newFile);
   11.50          printf("File size: %llu bytes\n", newFile.length());