IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 27303


Ignore:
Timestamp:
Mar 16, 2010, 3:04:24 PM (16 years ago)
Author:
Paul Price
Message:

Renaming psBitSet as psBits, and reworking code.

Location:
trunk/psLib
Files:
6 edited
3 moved

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/pslib_strict.h

    r23149 r27303  
    102102#include "psType.h"
    103103#include "psArray.h"
    104 #include "psBitSet.h"
     104#include "psBits.h"
    105105#include "psHash.h"
    106106#include "psList.h"
  • trunk/psLib/src/sys/psType.c

    r11617 r27303  
    2020
    2121#include "psType.h"
    22 #include "psBitSet.h"
     22#include "psBits.h"
    2323#include "psFits.h"
    2424#include "psPixels.h"
     
    4545        }
    4646        break;
    47     case PS_DATA_BITSET:
    48         if (psMemCheckBitSet(ptr)) {
     47    case PS_DATA_BITS:
     48        if (psMemCheckBits(ptr)) {
    4949            return true;
    5050        }
  • trunk/psLib/src/sys/psType.h

    r25256 r27303  
    107107    PS_DATA_STRING = 0x10000,          ///< psString (char *)
    108108    PS_DATA_ARRAY,                     ///< psArray
    109     PS_DATA_BITSET,                    ///< psBitSet
     109    PS_DATA_BITS,                      ///< psBits
    110110    PS_DATA_CUBE,                      ///< psCube
    111111    PS_DATA_FITS,                      ///< psFits
  • trunk/psLib/src/types/Makefile.am

    r23148 r27303  
    66libpslibtypes_la_SOURCES = \
    77        psArray.c \
    8         psBitSet.c \
     8        psBits.c \
    99        psHash.c \
    1010        psList.c \
     
    2323pkginclude_HEADERS = \
    2424        psArray.h \
    25         psBitSet.h \
     25        psBits.h \
    2626        psHash.h \
    2727        psList.h \
  • trunk/psLib/src/types/psBits.c

    r27302 r27303  
    1 /** @file  psBitSet.c
     1/** @file  psBits.c
    22 *
    33 *  @brief Creates an array of bytes of arbitrary length for storing individual bits.
     
    2626#include <math.h>
    2727
    28 #include "psBitSet.h"
     28#include "psBits.h"
    2929#include "psMemory.h"
    3030#include "psError.h"
     
    3434
    3535
    36 
    37 enum {
    38     UNKNOWN_OP,
    39     AND_OP,
    40     OR_OP,
    41     XOR_OP,
    42     NOT_OP
    43 };
    44 
    45 static void bitSetFree(psBitSet* inBitSet);
     36static void bitsFree(psBits *inBits);
    4637
    4738/** Private function to create a mask.
     
    5243 *  @return  char*: Pointer to byte in which bit is contained.
    5344 */
    54 PS_ATTR_PURE static char mask(psS32 bit)
    55 {
    56     char mask = (char)0x01;
    57 
    58     // Ignore splint warning about negative bit shifts
    59     /* @i@ */
    60     mask = mask << (bit % 8);
    61 
    62     return mask;
    63 }
    64 
    65 static void bitSetFree(psBitSet* inBitSet)
    66 {
    67     psFree(inBitSet->bits);
    68 }
    69 
    70 bool psMemCheckBitSet(psPtr ptr)
     45PS_ATTR_PURE static inline char mask(long bit)
     46{
     47    psAssert(bit < 8, "Bad bit: %ld", bit);
     48    return (char)0x01 << (bit % 8);
     49}
     50
     51// Return the byte with the bit of interest
     52static inline psU8 *bitsGetByte(const psBits *bits, long bit)
     53{
     54    return bits->bits + bit / 8;
     55}
     56
     57
     58static void bitsFree(psBits *inBits)
     59{
     60    psFree(inBits->bits);
     61}
     62
     63bool psMemCheckBits(psPtr ptr)
    7164{
    7265    PS_ASSERT_PTR(ptr, false);
    73     return ( psMemGetDeallocator(ptr) == (psFreeFunc)bitSetFree );
    74 }
    75 
    76 
    77 psBitSet* p_psBitSetAlloc(const char *file,
    78                           unsigned int lineno,
    79                           const char *func,
    80                           long nalloc)
    81 {
    82     if (nalloc < 0) {
    83         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    84                 _("The number of bit in a psBitSet (%ld) must be greater than zero."),
    85                 nalloc);
    86         return NULL;
    87     }
    88 
    89     psS32 numBytes = 0;
    90     psBitSet* newObj = NULL;
    91 
    92     numBytes = ceil(nalloc / 8.0);
    93     newObj = p_psAlloc(file, lineno, func, sizeof(psBitSet));
    94     psMemSetDeallocator(newObj, (psFreeFunc) bitSetFree);
    95     newObj->n = numBytes;
    96 
    97     // Ignore splint warning about releasing pointer members, since they've not been allocated yet
    98     /* @i@ */
    99     newObj->bits = psAlloc(sizeof(char) * numBytes);
    100 
    101     memset(newObj->bits, 0, numBytes);
    102 
    103     return newObj;
    104 }
    105 
    106 
    107 bool psBitSetSet(psBitSet* bitSet,
    108                       long bit)
    109 {
    110     unsigned char *byte = NULL;
    111 
    112     if (bitSet == NULL) {
    113         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    114                 _("Can not operate on a NULL psBitSet."));
    115         return false;
    116     } else if ( (bit < 0) ||
    117                 (bit > bitSet->n * 8 - 1) ) {
    118         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    119                 _("The specified bit position (%ld) is invalid.  Position must be between 0 and %ld."),
    120                 bit, bitSet->n * 8 - 1);
    121         return false;
    122     }
    123     // Variable byte is the byte in the array that contains the bit to be set
    124     byte = bitSet->bits + bit / 8;
     66    return (psMemGetDeallocator(ptr) == (psFreeFunc)bitsFree);
     67}
     68
     69
     70psBits *p_psBitsAlloc(const char *file, unsigned int lineno, const char *func, long nalloc)
     71{
     72    psAssert(nalloc >= 0, "The number of bits in a psBits (%ld) must be greater than zero.", nalloc);
     73
     74    int numBytes = ceil((float)nalloc / 8.0); // Number of bytes to use
     75    psBits *bits = p_psAlloc(file, lineno, func, sizeof(psBits));
     76    psMemSetDeallocator(bits, (psFreeFunc)bitsFree);
     77    bits->n = nalloc;
     78
     79    bits->bits = p_psAlloc(file, lineno, func, numBytes);
     80    memset(bits->bits, 0, numBytes);
     81
     82    return bits;
     83}
     84
     85
     86bool psBitsSet(psBits *bits, long bit)
     87{
     88    PS_ASSERT_BITS_NON_NULL(bits, false);
     89    PS_ASSERT_BITS_VALID_BIT(bits, bit, false);
     90
     91    psU8 *byte = bitsGetByte(bits, bit);  // Byte with the bit of interest
    12592    *byte |= mask(bit);
    12693
     
    12895}
    12996
    130 bool psBitSetClear(psBitSet* bitSet,
    131                         long bit)
    132 {
    133     unsigned char *byte = NULL;
    134 
    135     if (bitSet == NULL) {
    136         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    137                 _("Can not operate on a NULL psBitSet."));
    138         return false;
    139     } else if ( (bit < 0) ||
    140                 (bit > bitSet->n * 8 - 1) ) {
    141         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    142                 _("The specified bit position (%ld) is invalid.  Position must be between 0 and %ld."),
    143                 bit, bitSet->n * 8 - 1);
    144         return false;
    145     }
    146     // Variable byte is the byte in the array that contains the bit to be set
    147     byte = bitSet->bits + bit / 8;
    148     *byte &= ! mask(bit);
     97bool psBitsClear(psBits *bits, long bit)
     98{
     99    PS_ASSERT_BITS_NON_NULL(bits, false);
     100    PS_ASSERT_BITS_VALID_BIT(bits, bit, false);
     101
     102    psU8 *byte = bitsGetByte(bits, bit);  // Byte with the bit of interest
     103    *byte &= !mask(bit);
    149104
    150105    return true;
    151106}
    152107
    153 bool psBitSetTest(const psBitSet* bitSet,
    154                   long bit)
    155 {
    156     unsigned char *byte = NULL;
    157 
    158     if (bitSet == NULL) {
    159         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    160                 _("Can not operate on a NULL psBitSet."));
    161         return false;
    162     } else if ( (bit < 0) ||
    163                 (bit > bitSet->n * 8 - 1) ) {
    164         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    165                 _("The specified bit position (%ld) is invalid.  Position must be between 0 and %ld."),
    166                 bit,bitSet->n * 8 - 1);
    167         return false;
    168     }
    169 
    170     // Variable byte is the byte in the array that contains the bit to be tested
    171     byte = bitSet->bits + bit / 8;
     108
     109bool psBitsTest(const psBits *bits, long bit)
     110{
     111    // XXX These errors probably cannot be caught
     112    PS_ASSERT_BITS_NON_NULL(bits, false);
     113    PS_ASSERT_BITS_VALID_BIT(bits, bit, false);
     114
     115    psU8 *byte = bitsGetByte(bits, bit);  // Byte with the bit of interest
    172116    return ((*byte & mask(bit)) != 0);
    173117}
    174118
    175 psBitSet* psBitSetOp(psBitSet* outBitSet,
    176                      const psBitSet* inBitSet1,
    177                      const char *operator,
    178                      const psBitSet* inBitSet2)
    179 {
    180     if (inBitSet1 == NULL) {
    181         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    182                 _("First psBitSet operand can not be NULL."));
    183         psFree(outBitSet);
    184         return NULL;
    185     }
    186     if (operator == NULL) {
    187         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    188                 _("Specified operator is NULL.  Must specify desired operator."));
    189         psFree(outBitSet);
    190         return NULL;
    191     }
    192     psS32 i = 0;
    193     psS32 n = 0;
    194     unsigned char* outBits = NULL;
    195     unsigned char* inBits1 = NULL;
    196     unsigned char* inBits2 = NULL;
    197     psS32 op = UNKNOWN_OP;
    198 
    199     inBits1 = inBitSet1->bits;
    200 
    201 
    202     // parse the operator
    203     if (strcmp(operator,"AND")==0) {
     119psBits *psBitsOp(psBits *outBits, const psBits *bits1, const char *operator, const psBits *bits2)
     120{
     121    PS_ASSERT_BITS_NON_NULL(bits1, NULL);
     122    PS_ASSERT_STRING_NON_EMPTY(operator, NULL);
     123
     124    enum {
     125        UNKNOWN_OP,
     126        AND_OP,
     127        OR_OP,
     128        XOR_OP,
     129        NOT_OP
     130    } op = UNKNOWN_OP;
     131
     132    // Parse the operator
     133    if (strcmp(operator, "AND") == 0) {
    204134        op = AND_OP;
    205     } else if (strcmp(operator,"OR")==0) {
     135    } else if (strcmp(operator, "OR") == 0) {
    206136        op = OR_OP;
    207     } else if (strcmp(operator,"XOR")==0) {
     137    } else if (strcmp(operator, "XOR") == 0) {
    208138        op = XOR_OP;
    209     } else if (strcmp(operator,"NOT")==0) {
     139    } else if (strcmp(operator, "NOT") == 0) {
    210140        op = NOT_OP;
    211141    } else {
    212         psFree(outBitSet);
    213142        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    214                 _("Specified operator, %s, is invalid.  Valid operators are AND, OR, and XOR."),
     143                _("Specified operator, %s, is invalid.  Valid operators are AND, OR, XOR, NOT."),
    215144                operator);
    216145        return NULL;
     
    218147
    219148    if (op != NOT_OP) {
    220         if (inBitSet2 == NULL) {
    221             psError(PS_ERR_BAD_PARAMETER_NULL, true,
    222                     _("Second psBitSet operand can not be NULL."));
    223             psFree(outBitSet);
     149        PS_ASSERT_BITS_NON_NULL(bits2, false);
     150        if (bits1->n != bits2->n) {
     151            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
     152                    "The psBits operands must be the same size: %ld vs %ld",
     153                    bits1->n, bits2->n);
    224154            return NULL;
    225155        }
    226 
    227         if (inBitSet1->n != inBitSet2->n) {
    228             psError(PS_ERR_BAD_PARAMETER_SIZE, true,
    229                     _("The psBitSet operand must be the same size."));
    230             psFree(outBitSet);
    231             return NULL;
    232         }
    233         inBits2 = inBitSet2->bits;
    234     }
    235 
    236     if (outBitSet == NULL) {
    237         outBitSet = psBitSetAlloc(inBitSet1->n*8);
    238     } else if (outBitSet->n != inBitSet1->n) {
    239         outBitSet->n = inBitSet1->n;
    240         outBitSet->bits = psRealloc(outBitSet->bits, inBitSet1->n);
    241     }
    242 
    243     n = outBitSet->n;
    244     outBits = outBitSet->bits;
     156    }
     157
     158    int numBits = bits1->n;                   // Number of bits
     159    int numBytes = ceil((float)numBits / 8.0); // Number of bytes
     160
     161    if (!outBits) {
     162        outBits = psBitsAlloc(numBits);
     163    } else if (outBits->n != numBits) {
     164        outBits->n = numBits;
     165        outBits->bits = psRealloc(outBits->bits, numBytes);
     166    }
     167
     168    psU8 *in1 = bits1->bits;                  // Bits for input 1
     169    psU8 *in2 = (op == NOT_OP) ? NULL : bits2->bits; // Bits for input 2
     170    psU8 *out = outBits->bits;                       // Bits for output
    245171
    246172    switch (op) {
    247173    case AND_OP:
    248         for (i = 0; i < n; i++) {
    249             outBits[i] = inBits1[i] & inBits2[i];
     174        for (int i = 0; i < numBytes; i++) {
     175            out[i] = in1[i] & in2[i];
    250176        }
    251177        break;
    252178    case OR_OP:
    253         for (i = 0; i < n; i++) {
    254             outBits[i] = inBits1[i] | inBits2[i];
     179        for (int i = 0; i < numBytes; i++) {
     180            out[i] = in1[i] | in2[i];
    255181        }
    256182        break;
    257183    case XOR_OP:
    258         for (i = 0; i < n; i++) {
    259             outBits[i] = inBits1[i] ^ inBits2[i];
     184        for (int i = 0; i < numBytes; i++) {
     185            out[i] = in1[i] ^ in2[i];
    260186        }
    261187        break;
    262188    case NOT_OP:
    263189    default:
    264         for (i = 0; i < n; i++) {
    265             outBits[i] = ~inBits1[i];
    266         }
    267         break;
    268     }
    269 
    270     return outBitSet;
    271 }
    272 
    273 psBitSet* psBitSetNot(psBitSet* outBitSet,
    274                       const psBitSet* inBitSet)
    275 {
    276     if (inBitSet == NULL) {
    277         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    278                 _("Operand can not be NULL."));
    279         psFree(outBitSet);
    280         return NULL;
    281     }
    282 
    283     outBitSet = psBitSetOp(outBitSet,inBitSet,"NOT",NULL);
    284 
    285     return outBitSet;
    286 }
    287 
    288 psString psBitSetToString(const psBitSet* bitSet)
    289 {
    290     PS_ASSERT_PTR_NON_NULL(bitSet, NULL);
    291     psS32 i = 0;
    292     psS32 numBits = bitSet->n * 8;
    293     //    char *outString = psAlloc((size_t) numBits + 1);
    294     psString outString = psStringAlloc(numBits + 1);
    295 
    296     for (i = 0; i < numBits; i++) {
    297         outString[numBits - i - 1] = psBitSetTest(bitSet, i) ? '1' : '0';
    298     }
    299 
    300     outString[numBits] = 0;
    301 
    302     return outString;
    303 }
     190        for (int i = 0; i < numBytes; i++) {
     191            out[i] = ~in1[i];
     192        }
     193        break;
     194    }
     195
     196    return outBits;
     197}
     198
     199psBits *psBitsNot(psBits *outBits, const psBits *inBits)
     200{
     201    return psBitsOp(outBits, inBits, "NOT", NULL);
     202}
     203
     204psString psBitsToString(const psBits *bits)
     205{
     206    PS_ASSERT_BITS_NON_NULL(bits, NULL);
     207
     208    psS32 numBits = bits->n;
     209    psString string = psStringAlloc(numBits + 1);
     210
     211    char *out = &string[numBits];
     212    *out = '\0';
     213    out--;
     214    for (long i = 0; i < numBits; i++, out--) {
     215        *out = psBitsTest(bits, i) ? '1' : '0';
     216    }
     217
     218    return string;
     219}
  • trunk/psLib/src/types/psBits.h

    r27302 r27303  
    1 /** @file  psBitSet.h
     1/** @file  psBits.h
    22 *
    33 *  @brief Creates an array of bytes of arbitrary length for storing individual bits.
     
    1717 */
    1818
    19 #ifndef PSBITSET_H
    20 #define PSBITSET_H
     19#ifndef PSBITS_H
     20#define PSBITS_H
    2121
    2222#include "psType.h"
     
    3838typedef struct
    3939{
    40     long n;                            ///< Number of bytes in the array
    41     psU8 *bits;                        ///< Aray of bytes holding bits
     40    long n;                             ///< Number of bits in the array
     41    psU8 *bits;                         ///< Aray of bytes holding bits
    4242    psMutex lock;                       ///< Optional lock for thread safety
    43 }
    44 psBitSet;
     43} psBits;
    4544
    4645/*****************************************************************************/
     
    5352 *  datatype.
    5453 *
    55  *  @return bool:     True if the pointer matches a psBitSet structure, false otherwise.
     54 *  @return bool:     True if the pointer matches a psBits structure, false otherwise.
    5655 */
    57 bool psMemCheckBitSet(
     56bool psMemCheckBits(
    5857    psPtr ptr                          ///< the pointer whose type to check
    5958);
    6059
    6160
    62 /** Allocate a psBitSet.
     61/** Allocate a psBits.
    6362 *
    64  *  Create a psBitSet with the number of bits specified by the user. All bits
     63 *  Create a psBits with the number of bits specified by the user. All bits
    6564 *  are set to zero upon allocation.
    6665 *
    67  *  @return  psBitSet* : Pointer to struct containing array of bits and size of array.
     66 *  @return  psBits* : Pointer to struct containing array of bits and size of array.
    6867 */
    6968#ifdef DOXYGEN
    70 psBitSet* psBitSetAlloc(
    71     long nalloc                        ///< Number of bits in psBitSet array
     69psBits* psBitsAlloc(
     70    long nalloc                        ///< Number of bits in psBits array
    7271);
    7372#else // ifdef DOXYGEN
    74 psBitSet* p_psBitSetAlloc(
     73psBits* p_psBitsAlloc(
    7574    const char *file,                   ///< File of caller
    7675    unsigned int lineno,                ///< Line number of caller
    7776    const char *func,                   ///< Function name of caller
    78     long nalloc                        ///< Number of bits in psBitSet array
     77    long nalloc                        ///< Number of bits in psBits array
    7978) PS_ATTR_MALLOC;
    80 #define psBitSetAlloc(nalloc) \
    81       p_psBitSetAlloc(__FILE__, __LINE__, __func__, nalloc)
     79#define psBitsAlloc(nalloc) \
     80      p_psBitsAlloc(__FILE__, __LINE__, __func__, nalloc)
    8281#endif // ifdef DOXYGEN
    8382
     
    9089 *  with the first bit set in the zero bit slot of the zero element of the byte
    9190 *  array. As an example, setting bit 3 in an array with two elements would
    92  *  result in an psBitSet that looks like 00000000 00001000.
     91 *  result in an psBits that looks like 00000000 00001000.
    9392 *
    9493 *  @return  bool : Successful operation?
    9594 */
    96 bool psBitSetSet(
    97     psBitSet* bitSet,                  ///< Pointer to psBitSet to be set.
     95bool psBitsSet(
     96    psBits* bits,                  ///< Pointer to psBits to be set.
    9897    long bit                           ///< Bit to be set.
    9998);
     
    108107 *  @return  bool : Successful operation?
    109108 */
    110 bool psBitSetClear(
    111     psBitSet* bitSet,                  ///< Pointer to psBitSet to be cleared.
     109bool psBitsClear(
     110    psBits* bits,                  ///< Pointer to psBits to be cleared.
    112111    long bit                           ///< Bit to be cleared.
    113112);
     
    119118 *  resulting bit is based on a zero index format with the first bit set in the
    120119 *  zero bit slot of the zero element of the byte array.  As an example,
    121  *  testing bit 3 in a psBitSet with two bytes that looks like 00000000
     120 *  testing bit 3 in a psBits with two bytes that looks like 00000000
    122121 *  00001000 would return a value of one, since that is the value that was set.
    123122 *
    124123 *  @return  bool:      True if successful, otherwise false
    125124 */
    126 bool psBitSetTest(
    127     const psBitSet* bitSet,            ///< Pointer psBitSet to be tested.
     125bool psBitsTest(
     126    const psBits* bits,            ///< Pointer psBits to be tested.
    128127    long bit                           ///< Bit to be tested.
    129128);
    130129
    131130
    132 /** Perform a binary operation on two psBitSets
     131/** Perform a binary operation on two psBitss
    133132 *
    134  *  Perform an AND, OR, or XOR on two psBitSets. If the BitMasks are not the
     133 *  Perform an AND, OR, or XOR on two psBitss. If the BitMasks are not the
    135134 *  same size, the operation will not be performed and an error message will be
    136135 *  logged.
    137136 *
    138  *  @return  psBitSet* : Pointer to struct containing result of binary operation.
     137 *  @return  psBits* : Pointer to struct containing result of binary operation.
    139138 */
    140 psBitSet* psBitSetOp(
    141     psBitSet* outBitSet,               ///< Resulting psBitSet from binary operation
    142     const psBitSet* inBitSet1,         ///< First psBitSet on which to operate
     139psBits* psBitsOp(
     140    psBits* outBits,               ///< Resulting psBits from binary operation
     141    const psBits* inBits1,         ///< First psBits on which to operate
    143142    const char *operator,              ///< Bit operation
    144     const psBitSet* inBitSet2          ///< Second psBitSet on which to operate
     143    const psBits* inBits2          ///< Second psBits on which to operate
    145144);
    146145
    147146
    148 /** Perform a not operation on a psBitSet
     147/** Perform a not operation on a psBits
    149148 *
    150  *  Toggles bits in a psBitset. All zero bits are set to one and all one bits
     149 *  Toggles bits in a psBits. All zero bits are set to one and all one bits
    151150 *  are set to zero.
    152151 *
    153  *  @return  psBitSet* : Pointer to struct containing result of operation.
     152 *  @return  psBits* : Pointer to struct containing result of operation.
    154153 */
    155 psBitSet* psBitSetNot(
    156     psBitSet* outBitSet,               ///< Resulting psBitSet from operation
    157     const psBitSet* inBitSet           ///< Input psBitSet
     154psBits* psBitsNot(
     155    psBits* outBits,               ///< Resulting psBits from operation
     156    const psBits* inBits           ///< Input psBits
    158157);
    159158
    160159
    161 /** Convert the psBitSet to a string of ones and zeros.
     160/** Convert the psBits to a string of ones and zeros.
    162161 *
    163  *  Converts the contents of a psBitSet to a string representation of its
     162 *  Converts the contents of a psBits to a string representation of its
    164163 *  binary form of ones and zeros. The LSB is the right-most chracter. Each set
    165164 *  of eight characters represents one byte.
     
    167166 *  @return  psString:      Pointer to character array containing string data.
    168167 */
    169 psString psBitSetToString(
    170     const psBitSet* bitSet             ///< psBitSet to convert
     168psString psBitsToString(
     169    const psBits* bits             ///< psBits to convert
    171170);
     171
     172#define PS_ASSERT_BITS_NON_NULL(NAME, RVAL) \
     173if ((NAME) == NULL || (NAME)->bits == NULL || (NAME)->n < 0) { \
     174    psError(PS_ERR_BAD_PARAMETER_NULL, true, \
     175            "Unallowable operation: psBits %s or its data is NULL.", \
     176            #NAME); \
     177    return RVAL; \
     178} \
     179
     180#define PS_ASSERT_BITS_VALID_BIT(NAME, BIT, RVAL)              \
     181    if (BIT < 0 || BIT >= (NAME)->n) {     \
     182    psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
     183            "Unallowable operation: psBits %s or its data is NULL.", \
     184            #NAME); \
     185    return RVAL; \
     186} \
     187
    172188
    173189
    174190/// @}
    175 #endif // #ifndef PSBITSET_H
     191#endif // #ifndef PSBITS_H
  • trunk/psLib/src/types/psMetadata.c

    r26892 r27303  
    280280    }
    281281    case     PS_DATA_ARRAY:                     // psArray
    282     case     PS_DATA_BITSET:                    // psBitSet
     282    case     PS_DATA_BITS:                      // psBits
    283283    case     PS_DATA_CUBE:                      // psCube
    284284    case     PS_DATA_FITS:                      // psFits
  • trunk/psLib/test/types/Makefile.am

    r18145 r27303  
    2929        tap_psPixels_all \
    3030        tap_psHash_all \
    31         tap_psBitSet_all \
     31        tap_psBits_all \
    3232        tap_psList_all \
    3333        tap_psLookupTable_all \
  • trunk/psLib/test/types/tap_psBits_all.c

    r27302 r27303  
    11/**
    2  *  C Implementation: tap_psBitSet_all
    3  *
    4  * Description:  Tests for psBitSetAlloc, psBitSetSet, psMemCheckBitSet, psBitSetClear,
    5  *               psBitSetTest, psBitSetOp, psBitSetNot, psBitSetToString
     2 *  C Implementation: tap_psBits_all
     3 *
     4 * Description:  Tests for psBitsAlloc, psBitsSet, psMemCheckBits, psBitsClear,
     5 *               psBitsTest, psBitsOp, psBitsNot, psBitsToString
    66 *
    77 * Author: dRob <David.Robbins@mhpcc.hpc.mil>, (C) 2006
     
    2020    psLogSetFormat("HLNM");
    2121    psLogSetLevel(PS_LOG_INFO);
    22     plan_tests(31);
    23 
    24 
    25     // testBitSetBasics()
     22    plan_tests(30);
     23
     24
     25    // testBitsBasics()
    2626    {
    2727        psMemId id = psMemGetId();
    28         psBitSet *noBits = NULL;
    29         psBitSet *bs = NULL;
    30 
    31         //Return NULL for attempt to Allocate BitSet of negative size.
    32         {
    33             noBits = psBitSetAlloc(-1);
    34             ok( noBits == NULL,
    35                 "psBitSetAlloc:         return NULL for negative BitSet-size input.");
    36         }
    37         //Return properly allocated 0-size psBitSet
    38         {
    39             bs = psBitSetAlloc(0);
    40             ok( bs != NULL && psMemCheckBitSet(bs) && bs->n == 0,
    41                 "psBitSetAlloc:         return properly allocated psBitSet.");
    42         }
    43         //Return properly allocated psBitSet
     28        psBits *noBits = NULL;
     29        psBits *bs = NULL;
     30
     31        //Return properly allocated 0-size psBits
     32        {
     33            bs = psBitsAlloc(0);
     34            ok( bs != NULL && psMemCheckBits(bs) && bs->n == 0,
     35                "psBitsAlloc:         return properly allocated psBits.");
     36        }
     37        //Return properly allocated psBits
    4438        {
    4539            psFree(bs);
    46             bs = psBitSetAlloc(8);
    47             ok( bs != NULL && psMemCheckBitSet(bs) && bs->n == 1,
    48                 "psBitSetAlloc:         return properly allocated psBitSet.");
    49         }
    50         //Make sure psMemCheckBitSet works correctly - return false
     40            bs = psBitsAlloc(8);
     41            ok( bs != NULL && psMemCheckBits(bs) && bs->n == 8,
     42                "psBitsAlloc:         return properly allocated psBits.");
     43        }
     44        //Make sure psMemCheckBits works correctly - return false
    5145        if (0) {
    5246            int j = 2;
    53             ok( !psMemCheckBitSet(&j),
    54                 "psMemCheckBitSet:      return false for non-BitSet input.");
    55         }
    56 
    57         //BitSetSet Tests
    58         //Return FALSE for NULL input psBitSet
    59         {
    60             bool rc = psBitSetSet(NULL, 0);
     47            ok( !psMemCheckBits(&j),
     48                "psMemCheckBits:      return false for non-Bits input.");
     49        }
     50
     51        //BitsSet Tests
     52        //Return FALSE for NULL input psBits
     53        {
     54            bool rc = psBitsSet(NULL, 0);
    6155            ok(rc == false,
    62                 "psBitSetSet:           return FALSE for NULL BitSet input.");
     56                "psBitsSet:           return FALSE for NULL Bits input.");
    6357        }
    6458        //Return FALSE for negative bit input
    6559        {
    66             bool rc = psBitSetSet(bs, -1);
    67             ok( rc == false,
    68                 "psBitSetSet:           return TRUE for negative bits input.");
     60            bool rc = psBitsSet(bs, -1);
     61            ok( rc == false,
     62                "psBitsSet:           return TRUE for negative bits input.");
    6963            noBits = NULL;
    7064        }
     
    7266        {
    7367            psFree(bs);
    74             bs = psBitSetAlloc(8);
    75             bool rc = psBitSetSet(bs, 8);
    76             ok( rc == false,
    77                 "psBitSetSet:           return TRUE for out-of-range bits input.");
    78             noBits = NULL;
    79         }
    80 
    81         //Return set BitSet for valid inputs
    82         {
    83             psBitSetSet(bs, 2);
     68            bs = psBitsAlloc(8);
     69            bool rc = psBitsSet(bs, 8);
     70            ok( rc == false,
     71                "psBitsSet:           return TRUE for out-of-range bits input.");
     72            noBits = NULL;
     73        }
     74
     75        //Return set Bits for valid inputs
     76        {
     77            psBitsSet(bs, 2);
    8478            ok( bs->bits[0] == 4,
    85                 "psBitSetSet:           return properly set BitSet for valid inputs.");
    86         }
    87 
    88         //BitSetClear Tests
    89         //Return FALSE for NULL input psBitSet
    90         {
    91             bool rc = psBitSetClear(noBits, 0);
    92             ok( rc == false,
    93                 "psBitSetClear:         return FALSE for NULL BitSet input.");
     79                "psBitsSet:           return properly set Bits for valid inputs.");
     80        }
     81
     82        //BitsClear Tests
     83        //Return FALSE for NULL input psBits
     84        {
     85            bool rc = psBitsClear(noBits, 0);
     86            ok( rc == false,
     87                "psBitsClear:         return FALSE for NULL Bits input.");
    9488        }
    9589        //Return FALSE for negative bit input
    9690        {
    97             bool rc = psBitSetClear(bs, -1);
    98             ok( rc == false,
    99                 "psBitSetClear:        return TRUE for negative bits input.");
     91            bool rc = psBitsClear(bs, -1);
     92            ok( rc == false,
     93                "psBitsClear:        return TRUE for negative bits input.");
    10094            noBits = NULL;
    10195        }
    10296        //Return FALSE for out-of-range bits
    10397        {
    104             bool rc = psBitSetClear(bs, 8);
    105             ok( rc == false,
    106                 "psBitSetClear:        return FALSE for out-of-range bits input.");
    107             noBits = NULL;
    108         }
    109 
    110         //Return cleared BitSet for valid inputs
    111         {
    112             psBitSetClear(bs, 2);
     98            bool rc = psBitsClear(bs, 8);
     99            ok( rc == false,
     100                "psBitsClear:        return FALSE for out-of-range bits input.");
     101            noBits = NULL;
     102        }
     103
     104        //Return cleared Bits for valid inputs
     105        {
     106            psBitsClear(bs, 2);
    113107            ok( bs->bits[0] == 0,
    114                 "psBitSetClear:        return properly cleared BitSet for valid inputs.");
    115         }
    116 
    117         //BitSetTest Tests
    118         //Return false for NULL input psBitSet
    119         {
    120             ok( !psBitSetTest(noBits, 0),
    121                 "psBitSetTest:         return false for NULL BitSet input.");
     108                "psBitsClear:        return properly cleared Bits for valid inputs.");
     109        }
     110
     111        //BitsTest Tests
     112        //Return false for NULL input psBits
     113        {
     114            ok( !psBitsTest(noBits, 0),
     115                "psBitsTest:         return false for NULL Bits input.");
    122116        }
    123117        //Return false for negative bit input
    124118        {
    125             ok( !psBitSetTest(bs, -1),
    126                 "psBitSetTest:         return false for negative bits input.");
     119            ok( !psBitsTest(bs, -1),
     120                "psBitsTest:         return false for negative bits input.");
    127121        }
    128122        //Return false for out-of-range bits
    129123        {
    130             ok( !psBitSetTest(bs, 8),
    131                 "psBitSetTest:         return false for out-of-range bits input.");
    132         }
    133         //Return false for non-matching bit in BitSet
    134         {
    135             ok( !psBitSetTest(bs, 2),
    136                 "psBitSetTest:         return false for non-matching bit in BitSet.");
    137         }
    138         //Return false for non-matching bit in BitSet
    139         {
    140             psBitSetSet(bs, 2);
    141             ok( psBitSetTest(bs, 2),
    142                 "psBitSetTest:         return true for matching bit in BitSet.");
     124            ok( !psBitsTest(bs, 8),
     125                "psBitsTest:         return false for out-of-range bits input.");
     126        }
     127        //Return false for non-matching bit in Bits
     128        {
     129            ok( !psBitsTest(bs, 2),
     130                "psBitsTest:         return false for non-matching bit in Bits.");
     131        }
     132        //Return false for non-matching bit in Bits
     133        {
     134            psBitsSet(bs, 2);
     135            ok( psBitsTest(bs, 2),
     136                "psBitsTest:         return true for matching bit in Bits.");
    143137        }
    144138
     
    148142
    149143
    150     // testBitSetOps()
     144    // testBitsOps()
    151145    {
    152146        psMemId id = psMemGetId();
    153         psBitSet *noBits = NULL;
    154         psBitSet *bs = NULL;
    155         bs = psBitSetAlloc(8);
    156         psBitSetSet(bs, 2);  // 0000 0100 == 4
    157         psBitSetSet(bs, 3);  // 0000 1100 == 12
    158         psBitSetSet(bs, 4);  // 0001 1100 == 28
    159         psBitSetSet(bs, 5);  // 0011 1100 == 60
    160         psBitSetSet(bs, 6);  // 0111 1100 == 124
    161         psBitSetSet(bs, 7);  // 1111 1100 == 252
    162         psBitSet *out = NULL;
    163 
    164         //psBitSetNot Tests
    165         //Return NULL for NULL BitSet input
    166         {
    167             out = psBitSetNot(out, noBits);
    168             ok( out == NULL,
    169                 "psBitSetNot:          return NULL for NULL BitSet input.");
    170         }
    171         //Return correct BitSet for valid BitSet input
    172         {
    173             out = psBitSetNot(out, bs);  //bs = 1111 1100  so out should = 0000 0011 = 3
     147        psBits *noBits = NULL;
     148        psBits *bs = NULL;
     149        bs = psBitsAlloc(8);
     150        psBitsSet(bs, 2);  // 0000 0100 == 4
     151        psBitsSet(bs, 3);  // 0000 1100 == 12
     152        psBitsSet(bs, 4);  // 0001 1100 == 28
     153        psBitsSet(bs, 5);  // 0011 1100 == 60
     154        psBitsSet(bs, 6);  // 0111 1100 == 124
     155        psBitsSet(bs, 7);  // 1111 1100 == 252
     156        psBits *out = NULL;
     157
     158        //psBitsNot Tests
     159        //Return NULL for NULL Bits input
     160        {
     161            out = psBitsNot(out, noBits);
     162            ok( out == NULL,
     163                "psBitsNot:          return NULL for NULL Bits input.");
     164        }
     165        //Return correct Bits for valid Bits input
     166        {
     167            out = psBitsNot(out, bs);  //bs = 1111 1100  so out should = 0000 0011 = 3
    174168            ok( out->bits[0] == 3,
    175                 "psBitSetNot:          return correct BitSet for valid BitSet input.");
    176         }
    177 
    178         //psBitSetOp Tests   out = psBitSetOp(out, bs1, "op", bs2);
    179         //Return NULL for NULL BitSet input
     169                "psBitsNot:          return correct Bits for valid Bits input.");
     170        }
     171
     172        //psBitsOp Tests   out = psBitsOp(out, bs1, "op", bs2);
     173        //Return NULL for NULL Bits input
    180174        {
    181175            psFree(out);
    182176            out = NULL;
    183             out = psBitSetOp(out, noBits, "op", noBits);
    184             ok( out == NULL,
    185                 "psBitSetOp:           return NULL for NULL BitSet input.");
     177            out = psBitsOp(out, noBits, "op", noBits);
     178            ok( out == NULL,
     179                "psBitsOp:           return NULL for NULL Bits input.");
    186180        }
    187181        //Return NULL for NULL operator input
    188182        {
    189             out = psBitSetOp(out, bs, NULL, noBits);
    190             ok( out == NULL,
    191                 "psBitSetOp:           return NULL for NULL operator input.");
     183            out = psBitsOp(out, bs, NULL, noBits);
     184            ok( out == NULL,
     185                "psBitsOp:           return NULL for NULL operator input.");
    192186        }
    193187        //Return NULL for invalid operator input
    194188        {
    195             out = psBitSetOp(out, bs, "XAND", noBits);
    196             ok( out == NULL,
    197                 "psBitSetOp:           return NULL for invalid operator input.");
    198         }
    199         //Return NULL for AND operator with NULL second BitSet input
    200         {
    201             out = psBitSetOp(out, bs, "AND", noBits);
    202             ok( out == NULL,
    203                 "psBitSetOp:           return NULL for AND operator with NULL second BitSet input.");
    204         }
    205         //Return NULL for AND operator with BitSet inputs of differing size.
    206         psBitSet *bs2 = psBitSetAlloc(16);
    207         {
    208             out = psBitSetOp(out, bs, "AND", bs2);
    209             ok( out == NULL,
    210                 "psBitSetOp:           return NULL for AND operator with BitSet inputs of"
     189            out = psBitsOp(out, bs, "XAND", noBits);
     190            ok( out == NULL,
     191                "psBitsOp:           return NULL for invalid operator input.");
     192        }
     193        //Return NULL for AND operator with NULL second Bits input
     194        {
     195            out = psBitsOp(out, bs, "AND", noBits);
     196            ok( out == NULL,
     197                "psBitsOp:           return NULL for AND operator with NULL second Bits input.");
     198        }
     199        //Return NULL for AND operator with Bits inputs of differing size.
     200        psBits *bs2 = psBitsAlloc(16);
     201        {
     202            out = psBitsOp(out, bs, "AND", bs2);
     203            ok( out == NULL,
     204                "psBitsOp:           return NULL for AND operator with Bits inputs of"
    211205                " differing size.");
    212206        }
    213207        psFree(bs);
    214         bs = psBitSetAlloc(16);
    215         psBitSetSet(bs, 1);     // 0000 0010 == 2
    216         psBitSetSet(bs2, 2);   // 0000 0100 == 4
    217         //Return correct psBitSet output for valid inputs with AND operator
    218         {
    219             out = psBitSetOp(out, bs, "AND", bs2);
     208        bs = psBitsAlloc(16);
     209        psBitsSet(bs, 1);     // 0000 0010 == 2
     210        psBitsSet(bs2, 2);   // 0000 0100 == 4
     211        //Return correct psBits output for valid inputs with AND operator
     212        {
     213            out = psBitsOp(out, bs, "AND", bs2);
    220214            ok( out->bits[0] == 0,
    221                 "psBitSetOp:           return correct psBitSet output for valid inputs"
     215                "psBitsOp:           return correct psBits output for valid inputs"
    222216                " with AND operator.");
    223217        }
    224         //Return correct psBitSet output for valid inputs with OR operator
    225         {
    226             out = psBitSetOp(out, bs, "OR", bs2);
     218        //Return correct psBits output for valid inputs with OR operator
     219        {
     220            out = psBitsOp(out, bs, "OR", bs2);
    227221            ok( out->bits[0] == 6,
    228                 "psBitSetOp:           return correct psBitSet output for valid inputs"
     222                "psBitsOp:           return correct psBits output for valid inputs"
    229223                " with OR operator.");
    230224        }
    231         //Return correct psBitSet output for valid inputs with XOR operator
    232         psBitSetSet(bs2, 1);     // 0000 0110 == 6
    233         {
    234             out = psBitSetOp(out, bs, "XOR", bs2);
     225        //Return correct psBits output for valid inputs with XOR operator
     226        psBitsSet(bs2, 1);     // 0000 0110 == 6
     227        {
     228            out = psBitsOp(out, bs, "XOR", bs2);
    235229            ok( out->bits[0] == 4,
    236                 "psBitSetOp:           return correct psBitSet output for valid inputs"
     230                "psBitsOp:           return correct psBits output for valid inputs"
    237231                " with XOR operator.");
    238232        }
    239         //Return correct psBitSet output for valid inputs with NOT operator
     233        //Return correct psBits output for valid inputs with NOT operator
    240234        {
    241235            psFree(out);
    242             out = psBitSetAlloc(0);
    243             psBitSetSet(bs, 2);  // 0000 0110 == 4
    244             psBitSetSet(bs, 3);  // 0000 1110 == 12
    245             psBitSetSet(bs, 4);  // 0001 1110 == 28
    246             psBitSetSet(bs, 5);  // 0011 1110 == 60
    247             psBitSetSet(bs, 6);  // 0111 1110 == 124
    248             psBitSetSet(bs, 7);  // 1111 1110 == 252
    249             out = psBitSetOp(out, bs, "NOT", bs2);
     236            out = psBitsAlloc(0);
     237            psBitsSet(bs, 2);  // 0000 0110 == 4
     238            psBitsSet(bs, 3);  // 0000 1110 == 12
     239            psBitsSet(bs, 4);  // 0001 1110 == 28
     240            psBitsSet(bs, 5);  // 0011 1110 == 60
     241            psBitsSet(bs, 6);  // 0111 1110 == 124
     242            psBitsSet(bs, 7);  // 1111 1110 == 252
     243            out = psBitsOp(out, bs, "NOT", bs2);
    250244            ok( out->bits[0] == 1,
    251                 "psBitSetOp:           return correct psBitSet output for valid inputs"
     245                "psBitsOp:           return correct psBits output for valid inputs"
    252246                " with NOT operator.");
    253247        }
    254248
    255         //psBitSetToString Tests
    256         //Return NULL for NULL BitSet input
     249        //psBitsToString Tests
     250        //Return NULL for NULL Bits input
    257251        psString bitStr = NULL;
    258252        {
    259             bitStr = psBitSetToString(noBits);
     253            bitStr = psBitsToString(noBits);
    260254            ok( bitStr == NULL,
    261                 "psBitSetToString:     return NULL for NULL BitSet input.");
    262         }
    263         //Return correct string for valid BitSet input
     255                "psBitsToString:     return NULL for NULL Bits input.");
     256        }
     257        //Return correct string for valid Bits input
    264258        {
    265259            psFree(bs);
    266             bs = psBitSetAlloc(8);
    267             psBitSetSet(bs, 2);  // 0000 0100 == 4
    268             bitStr = psBitSetToString(bs);
     260            bs = psBitsAlloc(8);
     261            psBitsSet(bs, 2);  // 0000 0100 == 4
     262            bitStr = psBitsToString(bs);
    269263            ok( !strncmp(bitStr, "00000100", 10),
    270                 "psBitSetToString:     return correct string for valid BitSet input.");
     264                "psBitsToString:     return correct string for valid Bits input (%s).", bitStr);
    271265        }
    272266
Note: See TracChangeset for help on using the changeset viewer.