Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

TestFP.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Fri Apr 22 16:39:25 EDT 2005 by irina.furman@intel.com 
00020 //      modified on Fri Jul 16 17:15:03 EDT 2004 by ken@xorian.net  
00021 //      modified on Wed Sep  6 18:35:49 PDT 2000 by mann  
00022 //      modified on Mon Dec 15 15:15:17 PST 1997 by heydon
00023 
00024 #include <stdio.h>
00025 #include "FP.H"
00026 
00027 using std::cerr;
00028 using std::cout;
00029 using std::endl;
00030 
00031 void PrintFP(const FP::Tag& fp, const char *str) throw ()
00032 {
00033     cout << fp << " = \"" << str << "\"\n";
00034     cout.flush();
00035 }
00036 
00037 int main()
00038 {
00039   unsigned int failure_count = 0;
00040     int i;
00041 
00042     // Check size fields.
00043     if(PolyVal::ByteCnt != 16)
00044       {
00045         cerr << "PolyVal::ByteCnt is wrong (it should be 16, but is "
00046              << PolyVal::ByteCnt << ")" << endl;
00047         failure_count++;
00048       }
00049     if(FP::ByteCnt != 16)
00050       {
00051         cerr << "FP::ByteCnt is wrong (it should be 16, but is "
00052              << FP::ByteCnt << ")" << endl;
00053         failure_count++;
00054       }
00055     if(PolyVal::WordCnt != 2)
00056       {
00057         cerr << "PolyVal::WordCnt is wrong (it should be 2, but is "
00058              << PolyVal::WordCnt << ")" << endl;
00059         failure_count++;
00060       }
00061     if(FP::WordCnt != 2)
00062       {
00063         cerr << "FP::WordCnt is wrong (it should be 2, but is "
00064              << FP::WordCnt << ")" << endl;
00065         failure_count++;
00066       }
00067 
00068     // initialize the random number generator
00069     srandom(1);
00070 
00071     // Test empty string
00072     PrintFP(FP::Tag(""), "");
00073     cout << "\n";
00074 
00075     // Test known string
00076     const char *known = "The order is fpr, tag, untag";
00077     PrintFP(FP::Tag(known), known);
00078     cout << "\n";
00079 
00080     // Test that all results of fingerprinting a known string are the
00081     // same, regardless of alignment.
00082     cout << "Testing the effect of word-alignment skew" << endl << endl;
00083 
00084     const char *long_text = "A string longer than a word and a...",
00085       *short_text = "shorty";
00086     FP::Tag long_unskewed(long_text), short_unskewed(short_text);
00087 
00088     cout << "Unskewed tags:" << endl;
00089     PrintFP(long_unskewed, long_text);
00090     PrintFP(short_unskewed, short_text);
00091     cout << endl;
00092 
00093     Word *word_buff = NEW_ARRAY(Word, (strlen(long_text) / sizeof(Word)) + 2);
00094     for(i = 0; i < sizeof(Word); i++)
00095       {
00096         char *skewed = ((char *) word_buff) + i;
00097         strcpy(skewed, long_text);
00098         FP::Tag long_skewed(skewed);
00099         PrintFP(long_skewed, skewed);
00100         if(long_skewed != long_unskewed)
00101           {
00102             cerr << "  Skew test failed (long, i = " << i << ")!" << endl;
00103             failure_count++;
00104           }
00105         strcpy(skewed, short_text);
00106         FP::Tag short_skewed(skewed);
00107         PrintFP(short_skewed, skewed);
00108         if(short_skewed != short_unskewed)
00109           {
00110             cerr << "  Skew test failed (short, i = " << i << ")!" << endl;
00111             failure_count++;
00112           }
00113       }
00114 
00115     // Test that all results of "FP::Extend(FP(a), b)" are identical so long
00116     // as "Concat(a, b)" is the same string "s".
00117     cout << endl
00118          << "Testing FP::Extend(FP(a), b) where Concat(a, b) is identical"
00119          << endl << endl;
00120     const char *s = "Hello, World";
00121     const int sLen = strlen(s);
00122     FP::Tag s_base_tag(s);
00123     for (i = 0; i < sLen; i++) {
00124       FP::Tag s_tag(s, i);
00125       s_tag.Extend(s+i, sLen - i);
00126       PrintFP(s_tag, s);
00127       if(s_tag != s_base_tag)
00128         {
00129           cerr << "  Extend test failed (i = " << i << ")!" << endl;
00130           failure_count++;
00131         }
00132     }
00133     cout << "\n";
00134 
00135     // Test random buffer
00136     FP::Tag u, v;
00137     int buffer[4000], l1, l2, l3;
00138     char *p = (char *) buffer;
00139 
00140     for (i=0; i < 4000; i++) {
00141         buffer[i] = random();
00142     }
00143     cout << "Testing random strings\n\n";
00144     cout << "  Extend(Extend(FP(l1), l2), l3) vs. Extend(FP(l1), l2+l3)\n\n";
00145     for (i=0; i < 50; i++){
00146         l1 = random() & 0xfff;
00147         l2 = random() & 0xfff;
00148         l3 = random() & 0xfff;
00149         char buff[5];
00150         cout << "  l1 = "; sprintf(buff, "%4d", l1); cout << buff;
00151         cout << ", l2 = "; sprintf(buff, "%4d", l2); cout << buff;
00152         cout << ", l3 = "; sprintf(buff, "%4d", l3); cout << buff;
00153         cout << "    ";
00154         u = FP::Tag(p+l1, l2).Extend(p+(l1+l2), l3);
00155         v = FP::Tag(p+l1, l2+l3);
00156         if (u == v) {
00157             cout << "Worked!\n";
00158         } else {
00159             cout << "Broke!\n";
00160             PrintFP(u, "u");
00161             PrintFP(v, "v");
00162             break;
00163         }
00164     }
00165     cout << "\n";
00166 
00167     // Test fingerprints of '\000', '\001', and '\002'
00168     FP::Tag c0("\000", 1), c1("\001", 1), c2("\002", 1);
00169     PrintFP(c0, "\\000"); PrintFP(c1, "\\001"); PrintFP(c2, "\\002");
00170 
00171     // Test raw fingerprint operations
00172     RawFP fp;
00173     const char *init = "abcdefg";
00174     const char *extend = "hijklmnopqrs";
00175     FP::Tag t0(init);
00176     FP::Tag t1(init);
00177     FP::Tag t2(init);
00178     t0.Extend(extend);
00179     t1.Unpermute(/*OUT*/ fp);
00180     t1.ExtendRaw(/*INOUT*/ fp, extend, 3);
00181     t1.ExtendRaw(/*INOUT*/ fp, extend + 3);
00182     t1.Permute(fp);
00183     t2.Unpermute(/*OUT*/ fp);
00184     for (const char *ptr = extend; *ptr != '\0'; ptr++)
00185         t2.ExtendRaw(/*INOUT*/ fp, *ptr);
00186     t2.Permute(fp);
00187     if (t0 != t1) {
00188         cerr << "\nExtendRaw(string) failed!\n";
00189         failure_count++;
00190     }
00191     if (t0 != t2) {
00192         cerr << "\nExtendRaw(char) failed!\n";
00193         failure_count++;
00194     }
00195 
00196     // Test convertin fingerprints to/from bytes.
00197     FP::Tag from_source(p, sizeof(buffer));
00198     unsigned char tag_bytes[FP::ByteCnt];
00199     from_source.ToBytes(tag_bytes);
00200     FP::Tag from_bytes;
00201     from_bytes.FromBytes(tag_bytes);
00202 
00203     cout << endl
00204          << "from source = " << from_source << endl
00205          << "bytes       = { ";
00206     for(i = 0; i < FP::ByteCnt; i++)
00207       {
00208         char buff[5];
00209         sprintf(buff, "%02x", tag_bytes[i]);
00210         cout << buff;
00211         if(i < (FP::ByteCnt - 1))
00212           {
00213             cout << ", ";
00214           }
00215       }
00216     cout << " }" << endl
00217          << "from bytes  = " << from_bytes << endl << endl;
00218 
00219     if(from_bytes != from_source)
00220       {
00221         cerr << endl << "Conversion to/from bytes failed!" << endl << endl;
00222         failure_count++;
00223       }
00224 
00225     if(failure_count > 0)
00226       {
00227         exit(1);
00228       }
00229     else
00230       {
00231         cout << "All tests passed!" << endl;
00232       }
00233 
00234     return 0;
00235 }

Generated on Mon May 8 00:48:41 2006 for Vesta by  doxygen 1.4.2