Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/utility - RandomId.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 16 16 100.0 %
Date: 2012-04-09 Functions: 4 4 100.0 %
Branches: 13 18 72.2 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the RandomId class.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_UTILITY_RANDOM_ID_H
      23                 :            : #define __SSRC_WSPR_UTILITY_RANDOM_ID_H
      24                 :            : 
      25                 :            : #include <ssrc/wispers/utility/Random.h>
      26                 :            : 
      27                 :            : __BEGIN_NS_SSRC_WSPR_UTILITY
      28                 :            : 
      29                 :            : template<typename IntType>
      30   [ +  -  +  -  :         10 : class RandomId {
                   +  - ]
      31                 :            :   random_engine<IntType> _random;
      32                 :            : 
      33                 :            : public:
      34                 :            :   typedef IntType int_type;
      35                 :            : 
      36                 :         12 :   void operator()(string & id) const {
      37                 :            :     int_type data;
      38                 :            : #define TO_HEX(c) ((c) < 10 ? (c) + '0' : 'A' + (c) - 10)
      39                 :            : 
      40                 :         12 :     unsigned int i = id.size();
      41                 :            : 
      42         [ +  + ]:         72 :     while(i >= 2) {
      43                 :         48 :       data = _random();
      44                 :            : 
      45         [ +  + ]:        240 :       for(unsigned int j = 0; j < sizeof(int_type); ++j) {
      46                 :        192 :         char c1 = ((data & 0xf0) >> 4);
      47                 :        192 :         char c2 = (data & 0x0f);
      48         [ +  + ]:        192 :         id[--i] = TO_HEX(c1);
      49         [ +  + ]:        192 :         id[--i] = TO_HEX(c2);
      50                 :        192 :         data >>= 8;
      51                 :            :       }
      52                 :            :     }
      53                 :            : 
      54                 :            : #undef TO_HEX
      55                 :         12 :   }
      56                 :            : 
      57                 :          6 :   string operator()(const unsigned int bytes) const {
      58         [ +  - ]:          6 :     string str(bytes, ' ');
      59         [ +  - ]:          6 :     operator()(str);
      60                 :          6 :     return str;
      61                 :            :   }
      62                 :            : 
      63                 :            : };
      64                 :            : 
      65                 :            : __END_NS_SSRC_WSPR_UTILITY
      66                 :            : 
      67                 :            : #endif