Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:16

0001 /*
0002  * Copyright (c) 2019 Opticks Team. All Rights Reserved.
0003  *
0004  * This file is part of Opticks
0005  * (see https://bitbucket.org/simoncblyth/opticks).
0006  *
0007  * Licensed under the Apache License, Version 2.0 (the "License"); 
0008  * you may not use this file except in compliance with the License.  
0009  * You may obtain a copy of the License at
0010  *
0011  *   http://www.apache.org/licenses/LICENSE-2.0
0012  *
0013  * Unless required by applicable law or agreed to in writing, software 
0014  * distributed under the License is distributed on an "AS IS" BASIS, 
0015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0016  * See the License for the specific language governing permissions and 
0017  * limitations under the License.
0018  */
0019 
0020 #include <string>
0021 #include <cstring>
0022 #include <cassert>
0023 #include <iostream>
0024 #include <fstream>
0025 
0026 #include "SSys.hh"
0027 #include "SDigest.hh"
0028 #include "OPTICKS_LOG.hh"
0029 
0030 
0031 
0032 void test_Buffer()
0033 {
0034    char buf[6] ; 
0035    buf[0] = 'h' ; 
0036    buf[1] = 'e' ; 
0037    buf[2] = 'l' ; 
0038    buf[3] = 'l' ; 
0039    buf[4] = 'o' ; 
0040    buf[5] = '\0' ; 
0041 
0042    std::string dig = SDigest::Buffer( buf, 5 ); 
0043 
0044    LOG(info) << buf
0045               << " --> "
0046               << dig
0047               ;
0048 }
0049 
0050 
0051 
0052 void test_static()
0053 {
0054     const char* str = "hello" ;
0055     unsigned int len = strlen(str);
0056     void* buf = (void*)str ; 
0057 
0058     std::string a = SDigest::md5digest(str, len );
0059     std::string b = SDigest::digest(buf, len );
0060     assert( a.compare(b) == 0 );
0061 
0062     LOG(info) << str
0063               << " --> "
0064               << a 
0065               ;
0066 }
0067 
0068 void test_update()
0069 {
0070    const char* str = "hello" ;
0071    SDigest dig ; 
0072    dig.update( const_cast<char*>(str), sizeof(char)*strlen(str) );
0073    char* dgst = dig.finalize();
0074 
0075    LOG(info) << str 
0076              << " --> "
0077              << dgst 
0078              ;
0079 }
0080 
0081 
0082 void test_digest_vec()
0083 {
0084    std::vector<std::string> v ; 
0085    v.push_back("red");
0086    v.push_back("green");
0087    v.push_back("blue");
0088 
0089    std::string dig0 = SDigest::digest(v); 
0090 
0091 
0092    v.push_back("blue");
0093    v.push_back("blue");
0094    v.push_back("blue");
0095    v.push_back("blue");
0096    v.push_back("blue");
0097 
0098    std::string dig1 = SDigest::digest(v); 
0099    std::string dig2 = SDigest::digest_skipdupe(v); 
0100 
0101    LOG(info) 
0102         << " dig0 " << dig0
0103         << " dig1 " << dig1
0104         << " dig2 " << dig2
0105         ;
0106 
0107 
0108    assert( dig0.compare(dig1.c_str()) != 0 );
0109    assert( dig0.compare(dig2.c_str()) == 0 );
0110 
0111 
0112 }
0113 
0114 void test_IsDigest()
0115 {
0116     assert( SDigest::IsDigest(NULL) == false );
0117     assert( SDigest::IsDigest("0123") == false );
0118     assert( SDigest::IsDigest("0123456789abcdef") == false );
0119     assert( SDigest::IsDigest("0123456789abcdef0123456789abcdef") == true );
0120 }
0121 
0122 void test_DigestPath(const char* path)
0123 {
0124     std::string d0 = SDigest::DigestPath(path) ; 
0125     std::string d1 = SDigest::DigestPath2(path) ; 
0126     assert( d0.compare(d1) == 0 ); 
0127 
0128     if(SSys::getenvint("VERBOSE",0) == 1)
0129     { 
0130         std::cout << "SDigest::DigestPath  "  << d0 << std::endl ; 
0131         std::cout << "SDigest::DigestPath2 "  << d1 << std::endl ; 
0132     }
0133     std::cout << d0 << std::endl ;
0134 }
0135 
0136 
0137 
0138 void test_DigestPathInByteRange(const char* path, int i0, int i1)
0139 {
0140     LOG(info) 
0141         << " path " << path 
0142         << " i0 " << i0
0143         << " i1 " << i1
0144         ;
0145         
0146     std::string d0 = SDigest::DigestPathInByteRange(path, i0, i1 ) ; 
0147     std::cout << d0 << std::endl ;
0148 }
0149 
0150 void test_DigestPathInByteRange()
0151 {
0152     unsigned n = 100 ;  
0153     const char* path = "/tmp/test_DigestPathInByteRange.txt" ; 
0154     const char* a = "0123456789abcdef" ; 
0155     unsigned bufsize = 10 ; 
0156 
0157 
0158     std::ofstream out(path, std::ios::out);
0159     for(unsigned i=0 ; i < n ; i++ ) out << a ; 
0160     out.close(); 
0161 
0162     LOG(info) 
0163         << " path " << path 
0164         << " bufsize " << bufsize 
0165         ; 
0166 
0167     std::string d0, d1 ; 
0168 
0169     for(unsigned i=0 ; i < n ; i++)
0170     { 
0171         int i0 = i*strlen(a) ; 
0172         int i1 = (i+1)*strlen(a) ; 
0173 
0174         if(d0.empty()) 
0175         {
0176             d0 = SDigest::DigestPathInByteRange(path, i0, i1, bufsize ) ; 
0177             continue ; 
0178         }
0179              
0180         d1 = SDigest::DigestPathInByteRange(path, i0, i1, bufsize ) ;
0181 
0182         bool match = d0.compare(d1) == 0 ;  
0183 
0184 
0185         LOG(info) 
0186             << " i "  << std::setw(5) << i  
0187             << " i0 "  << std::setw(5) << i0  
0188             << " i1 "  << std::setw(5) << i1  
0189             << " d0 " << d0 
0190             << " d1 " << d1
0191             << ( !match ? " MISMATCH " : "" ) 
0192             ; 
0193 
0194         assert( match ); 
0195     }
0196 
0197 }
0198 
0199 
0200 void test_main( int argc, char** argv )
0201 {
0202     const char* path = argc > 1 ? argv[1] : argv[0] ; 
0203     int i0 = argc > 2 ? atoi(argv[2]) : -1 ; 
0204     int i1 = argc > 3 ? atoi(argv[3]) : -1 ; 
0205 
0206     if( i0 > -1 && i1 > -1 ) 
0207     {
0208         test_DigestPathInByteRange(path, i0, i1);
0209     } 
0210     else
0211     {
0212         test_DigestPath(path);
0213     }
0214 }
0215 
0216 
0217 
0218 int main(int argc, char** argv)
0219 {
0220     OPTICKS_LOG(argc, argv);
0221 
0222     test_Buffer(); 
0223     test_static();
0224     test_update();
0225     /*
0226     test_digest_vec();
0227     test_IsDigest();
0228     test_main(argc, argv);
0229     test_DigestPathInByteRange(); 
0230     */
0231 
0232 
0233     return 0 ; 
0234 }
0235