IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 965


Ignore:
Timestamp:
Jun 9, 2004, 2:29:09 PM (22 years ago)
Author:
harman
Message:

Added more error checks

Location:
trunk/psLib
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/collections/psBitSet.c

    r952 r965  
    1010 *  @author Ross Harman, MHPCC
    1111 *
    12  *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-06-09 21:18:24 $
     12 *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-06-10 00:29:09 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2222#include <stdio.h>
    2323#include <ctype.h>
     24#include <math.h>
    2425
    2526#include "psBitSet.h"
     
    7778psBitSet* psBitSetAlloc(int n)
    7879{
     80    int numBytes = 0;
    7981    psBitSet *newObj = NULL;
    8082
     
    8486    }
    8587
     88    numBytes = ceil(n/8.0);
    8689    newObj = psAlloc(sizeof(psBitSet));
    87     newObj->n = n;
     90    newObj->n = numBytes;
    8891
    8992    // Ignore splint warning about releasing pointer members, since they've not been allocated yet
    9093    /*@i@*/
    91     newObj->bits = psAlloc(sizeof(char)*n);
    92     memset(newObj->bits, n, 0);
     94    newObj->bits = psAlloc(sizeof(char)*numBytes);
     95    memset(newObj->bits, numBytes, 0);
    9396
    9497    return newObj;
     
    113116        return inBitSet;
    114117    } else if(bit < 0) {
    115         psError(__func__, " : Line %d - Negative bit position not allowed: %d\n", __LINE__, bit);
     118        psError(__func__, " : Line %d - Bit position too small: %d\n", __LINE__, bit);
     119        return inBitSet;
     120    } else if(bit > inBitSet->n*8-1) {
     121        psError(__func__, " : Line %d - Bit position too large: %d\n", __LINE__, bit);
    116122        return inBitSet;
    117123    }
     
    132138        return 0;
    133139    } else if(bit < 0) {
    134         psError(__func__, " : Line %d - Negative bit position not allowed: %d\n", __LINE__, bit);
     140        psError(__func__, " : Line %d - Bit position too small: %d\n", __LINE__, bit);
     141        return 0;
     142    } else if(bit > inBitSet->n*8-1) {
     143        psError(__func__, " : Line %d - Bit position too large: %d\n", __LINE__, bit);
    135144        return 0;
    136145    }
  • trunk/psLib/src/collections/psBitSet.h

    r614 r965  
    44 *
    55 *  Bit masks are useful tools for toggling various flags and options. This set of functions module provides
    6  *  a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary 
     6 *  a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary
    77 *  operations. A print function is also provided to display the entire set of bits in binary format as a
    88 *  string.
    99 *
    1010 *  @author Ross Harman, MHPCC
    11  *   
    12  *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-05-08 00:08:27 $
     11 *
     12 *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-06-10 00:28:58 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2525/** Struct containing array of bytes to hold bit data and corresponding array length.
    2626 *
    27  *  The bits in the struct are assembled in as an array of bytes with eight bits per byte. The bits are 
     27 *  The bits in the struct are assembled in as an array of bytes with eight bits per byte. The bits are
    2828 *  arranged with the LSB in first (right most) position of the first array element.
    2929 */
     
    4141/** Allocate a psBitSet.
    4242 *
    43  *  Create a psBitSet with the number of bytes specified by the user. All bits are set to zero upon 
     43 *  Create a psBitSet with the number of bytes specified by the user. All bits are set to zero upon
    4444 *  allocation.
    4545 *
     
    4848/*@null@*/
    4949psBitSet* psBitSetAlloc(
    50     int n   /**< Number of bytes in psBitSet array */
     50    int n   /**< Number of bits in psBitSet array */
    5151);
    5252
     
    6161/** Set a bit.
    6262 *
    63  *  Sets a bit at a given bit location, either one or zero. The bit is set based on a zero index with the 
     63 *  Sets a bit at a given bit location, either one or zero. The bit is set based on a zero index with the
    6464 *  first bit set in the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in
    6565 *  an array with two elements would result in an psBitSet that looks like 00000000 00001000.
     
    7474/** Test the value of a bit.
    7575 *
    76  *  Prints the value of a bit at a given bit location, either one or zero. The resulting bit is based on a 
    77  *  zero index format with the first bit set in the zero bit slot of the zero element of the byte array 
     76 *  Prints the value of a bit at a given bit location, either one or zero. The resulting bit is based on a
     77 *  zero index format with the first bit set in the zero bit slot of the zero element of the byte array
    7878 *  As an example, testing bit 3 in a psBitSet with two bytes that looks like 00000000 00001000 would return a
    7979 *  value of one, since that is the value that was set.
  • trunk/psLib/src/types/psBitSet.c

    r952 r965  
    1010 *  @author Ross Harman, MHPCC
    1111 *
    12  *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-06-09 21:18:24 $
     12 *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-06-10 00:29:09 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2222#include <stdio.h>
    2323#include <ctype.h>
     24#include <math.h>
    2425
    2526#include "psBitSet.h"
     
    7778psBitSet* psBitSetAlloc(int n)
    7879{
     80    int numBytes = 0;
    7981    psBitSet *newObj = NULL;
    8082
     
    8486    }
    8587
     88    numBytes = ceil(n/8.0);
    8689    newObj = psAlloc(sizeof(psBitSet));
    87     newObj->n = n;
     90    newObj->n = numBytes;
    8891
    8992    // Ignore splint warning about releasing pointer members, since they've not been allocated yet
    9093    /*@i@*/
    91     newObj->bits = psAlloc(sizeof(char)*n);
    92     memset(newObj->bits, n, 0);
     94    newObj->bits = psAlloc(sizeof(char)*numBytes);
     95    memset(newObj->bits, numBytes, 0);
    9396
    9497    return newObj;
     
    113116        return inBitSet;
    114117    } else if(bit < 0) {
    115         psError(__func__, " : Line %d - Negative bit position not allowed: %d\n", __LINE__, bit);
     118        psError(__func__, " : Line %d - Bit position too small: %d\n", __LINE__, bit);
     119        return inBitSet;
     120    } else if(bit > inBitSet->n*8-1) {
     121        psError(__func__, " : Line %d - Bit position too large: %d\n", __LINE__, bit);
    116122        return inBitSet;
    117123    }
     
    132138        return 0;
    133139    } else if(bit < 0) {
    134         psError(__func__, " : Line %d - Negative bit position not allowed: %d\n", __LINE__, bit);
     140        psError(__func__, " : Line %d - Bit position too small: %d\n", __LINE__, bit);
     141        return 0;
     142    } else if(bit > inBitSet->n*8-1) {
     143        psError(__func__, " : Line %d - Bit position too large: %d\n", __LINE__, bit);
    135144        return 0;
    136145    }
  • trunk/psLib/src/types/psBitSet.h

    r614 r965  
    44 *
    55 *  Bit masks are useful tools for toggling various flags and options. This set of functions module provides
    6  *  a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary 
     6 *  a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary
    77 *  operations. A print function is also provided to display the entire set of bits in binary format as a
    88 *  string.
    99 *
    1010 *  @author Ross Harman, MHPCC
    11  *   
    12  *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-05-08 00:08:27 $
     11 *
     12 *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-06-10 00:28:58 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2525/** Struct containing array of bytes to hold bit data and corresponding array length.
    2626 *
    27  *  The bits in the struct are assembled in as an array of bytes with eight bits per byte. The bits are 
     27 *  The bits in the struct are assembled in as an array of bytes with eight bits per byte. The bits are
    2828 *  arranged with the LSB in first (right most) position of the first array element.
    2929 */
     
    4141/** Allocate a psBitSet.
    4242 *
    43  *  Create a psBitSet with the number of bytes specified by the user. All bits are set to zero upon 
     43 *  Create a psBitSet with the number of bytes specified by the user. All bits are set to zero upon
    4444 *  allocation.
    4545 *
     
    4848/*@null@*/
    4949psBitSet* psBitSetAlloc(
    50     int n   /**< Number of bytes in psBitSet array */
     50    int n   /**< Number of bits in psBitSet array */
    5151);
    5252
     
    6161/** Set a bit.
    6262 *
    63  *  Sets a bit at a given bit location, either one or zero. The bit is set based on a zero index with the 
     63 *  Sets a bit at a given bit location, either one or zero. The bit is set based on a zero index with the
    6464 *  first bit set in the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in
    6565 *  an array with two elements would result in an psBitSet that looks like 00000000 00001000.
     
    7474/** Test the value of a bit.
    7575 *
    76  *  Prints the value of a bit at a given bit location, either one or zero. The resulting bit is based on a 
    77  *  zero index format with the first bit set in the zero bit slot of the zero element of the byte array 
     76 *  Prints the value of a bit at a given bit location, either one or zero. The resulting bit is based on a
     77 *  zero index format with the first bit set in the zero bit slot of the zero element of the byte array
    7878 *  As an example, testing bit 3 in a psBitSet with two bytes that looks like 00000000 00001000 would return a
    7979 *  value of one, since that is the value that was set.
  • trunk/psLib/test/collections/tst_psBitSet_01.c

    r717 r965  
    55 *  This test driver contains the following tests for psArray test point 1:
    66 *     A)  Create psBitSet
    7  *     B)  Set single bit
    8  *     C)  Test single bit
    9  *     D)  Free psBitSet
     7 *     B)  Set bits
     8 *     C)  Test bits
     9 *     D)  Attempt to test negative bit
     10 *     E)  Attempt to test bit to large
     11 *     F)  Attempt to test bit in null BitSet
     12 *     G)  Attempt to set negative bit
     13 *     H)  Attempt to set bit to large
     14 *     I)  Attempt to set bit in null BitSet
     15 *     J)  Free psBitSet
    1016 *
    1117 *  @author  Ross Harman, MHPCC
    1218 *
    13  *  @version $Revision: 1.3 $  $Name: not supported by cvs2svn $
    14  *  @date  $Date: 2004-05-18 19:22:34 $
     19 *  @version $Revision: 1.4 $  $Name: not supported by cvs2svn $
     20 *  @date  $Date: 2004-06-10 00:28:23 $
    1521 *
    1622 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2531{
    2632    char *binOut = NULL;
     33    psBitSet *tempBs = NULL;
    2734
    2835
     
    3037    printPositiveTestHeader(stdout,"psBitSet", "Create psBitSet");
    3138    printf("Creating psBitSet with 24 bits...\n");
    32     psBitSet* bs = psBitSetAlloc(3);
     39    psBitSet* bs = psBitSetAlloc(24);
    3340    binOut = psBitSetToString(bs);
    3441    printf("%s\n\n", binOut);
     
    3744
    3845
    39     // Test B - Set single bit
    40     printPositiveTestHeader(stdout, "psBitSet", "Set single bit");
     46    // Test B - Set bits
     47    printPositiveTestHeader(stdout, "psBitSet", "Set bits");
     48    tempBs = bs;
     49    printf("Setting first bit...\n");
     50    bs = psBitSetSet(bs, 0);
    4151    printf("Setting third bit...\n");
    4252    bs = psBitSetSet(bs, 2);
     53    printf("Setting last bit...\n");
     54    bs = psBitSetSet(bs, 23);
     55    if(bs != tempBs) {
     56        printf("Error: Return pointer not equal to output argument pointer\n");
     57    }
    4358    binOut = psBitSetToString(bs);
    4459    printf("%s\n\n", binOut);
    4560    psFree(binOut);
    46     printFooter(stdout, "psBitSet", "Set single bit", true);
     61    printFooter(stdout, "psBitSet", "Set bits", true);
    4762
    4863
    49     // Test C - Test single bit
    50     printPositiveTestHeader(stdout, "psBitSet", "Test single bit");
     64    // Test C - Test bits
     65    printPositiveTestHeader(stdout, "psBitSet", "Test bits");
     66    printf("Testing first bit...\n");
     67    printf("Result: %d\n", psBitSetTest(bs, 0));
    5168    printf("Testing third bit...\n");
    5269    printf("Result: %d\n", psBitSetTest(bs, 2));
    53     printFooter(stdout, "psBitSet", "Test single bit", true);
     70    printf("Testing last bit...\n");
     71    printf("Result: %d\n", psBitSetTest(bs, 23));
     72    printFooter(stdout, "psBitSet", "Test bits", true);
    5473
    5574
    56     // Test D - Free psBitSet
     75    // Test D - Attempt to test negative bit
     76    printNegativeTestHeader(stdout,"psBitSet", "Attempt to test negative bit",
     77                            "Bit position too small: -4", 0);
     78    if(psBitSetTest(bs, -4)) {
     79        printf("Error: Return value should be zero\n");
     80    }
     81    printFooter(stdout, "psBitSet", "Attempt to test negative bit", true);
     82
     83
     84    // Test E - Attempt to test bit to large
     85    printNegativeTestHeader(stdout,"psBitSet", "Attempt to test bit to large",
     86                            "Bit position too large: 200", 0);
     87    if(psBitSetTest(bs, 200)) {
     88        printf("Error: Return value should be zero\n");
     89    }
     90    printFooter(stdout, "psBitSet", "Attempt to test bit to large", true);
     91
     92
     93    // Test F - Attempt to test bit in null BitSet
     94    printNegativeTestHeader(stdout,"psBitSet", "Attempt to test bit in null BitSet",
     95                            "Null psBitSet for inBitSet argument", 0);
     96    psBitSetTest(NULL, 0);
     97    printFooter(stdout, "psBitSet", "Attempt to test bit in null BitSet", true);
     98
     99
     100    // Test G - Attempt to set negative bit
     101    printNegativeTestHeader(stdout,"psBitSet", "Attempt to set negative bit",
     102                            "Bit position too small: -4", 0);
     103    bs = psBitSetSet(bs, -4);
     104    printFooter(stdout, "psBitSet", "Attempt to set negative bit", true);
     105
     106
     107    // Test H - Attempt to set bit to large
     108    printNegativeTestHeader(stdout,"psBitSet", "Attempt to set bit to large",
     109                            "Bit position too large: 200", 0);
     110    bs = psBitSetSet(bs, 200);
     111    printFooter(stdout, "psBitSet", "Attempt to set bit to large", true);
     112
     113
     114    // Test I - Attempt to set bit in null BitSet
     115    printNegativeTestHeader(stdout,"psBitSet", "Attempt to set bit in null BitSet",
     116                            "Null psBitSet for inBitSet argument", 0);
     117    psBitSetSet(NULL, 0);
     118    printFooter(stdout, "psBitSet", "Attempt to set bit in null BitSet", true);
     119
     120
     121    // Test J - Free psBitSet
    57122    printPositiveTestHeader(stdout, "psBitSet", "Free psBitSet");
    58123    psBitSetFree(bs);
  • trunk/psLib/test/collections/tst_psBitSet_05.c

    r717 r965  
    1010 *  @author  Ross Harman, MHPCC
    1111 *
    12  *  @version $Revision: 1.3 $  $Name: not supported by cvs2svn $
    13  *  @date  $Date: 2004-05-18 19:22:34 $
     12 *  @version $Revision: 1.4 $  $Name: not supported by cvs2svn $
     13 *  @date  $Date: 2004-06-10 00:28:36 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2828    // Test A - Create two psBitSets
    2929    printPositiveTestHeader(stdout,"psBitSet", "Create two psBitSets of different size");
    30     psBitSet* bs1 = psBitSetAlloc(3);
    31     psBitSet* bs2 = psBitSetAlloc(5);
     30    psBitSet* bs1 = psBitSetAlloc(24);
     31    psBitSet* bs2 = psBitSetAlloc(40);
    3232    binOut = psBitSetToString(bs1);
    3333    printf("%s\n", binOut);
     
    4141    // Test B - Attempt OR with psBitsets, should get error
    4242    printNegativeTestHeader(stdout, "psBitSet", "Attempt OR with psBitsets", "psBitSet sizes not the same", 0);
    43     psBitSet* outbs = psBitSetAlloc(3);
     43    psBitSet* outbs = psBitSetAlloc(24);
    4444    outbs = psBitSetOp(outbs, bs1, "OR", bs2);
    4545    printFooter(stdout, "psBitSet", "Attempt OR with psBitsets", true);
  • trunk/psLib/test/collections/tst_psBitSet_06.c

    r957 r965  
    1212 *  @author  Ross Harman, MHPCC
    1313 *
    14  *  @version $Revision: 1.5 $  $Name: not supported by cvs2svn $
    15  *  @date  $Date: 2004-06-09 21:29:56 $
     14 *  @version $Revision: 1.6 $  $Name: not supported by cvs2svn $
     15 *  @date  $Date: 2004-06-10 00:28:43 $
    1616 *
    1717 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3030    // Test A - Create two psBitSets
    3131    printPositiveTestHeader(stdout,"psBitSet", "Create two psBitSets");
    32     psBitSet* bs1 = psBitSetAlloc(3);
    33     psBitSet* bs2 = psBitSetAlloc(3);
     32    psBitSet* bs1 = psBitSetAlloc(24);
     33    psBitSet* bs2 = psBitSetAlloc(24);
    3434    binOut = psBitSetToString(bs1);
    3535    printf("%s\n", binOut);
     
    4444    printNegativeTestHeader(stdout,"psBitSet", "Perform invalid binary operation",
    4545                            "Invalid psBitMask binary operation", 0);
    46     psBitSet* outbs = psBitSetAlloc(3);
     46    psBitSet* outbs = psBitSetAlloc(24);
    4747    outbs = psBitSetOp(outbs, bs1, "ZZXOR", bs2);
    4848    printFooter(stdout, "psBitSet", "Perform binary XOR with psBitSets", true);
Note: See TracChangeset for help on using the changeset viewer.