Just a function declaration

Just wanna ask what kind of the coding should implement in this function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* 
 * File:   Bitmap.h
 * Author: nabg
 *
 * Created on 26 February 2009, 1:20 PM
 */

#ifndef _BITMAP_H
#define	_BITMAP_H

// Code assumes 32-bit unsigned long integers
#define MAXBITS 512
#define NUMWORDS 16

typedef unsigned long Bits;

class Bitmap {
public:
	Bitmap();
	void	Zero(void);
	void	SetBit(int bitnum);
	void	ClearBit(int bitnum);
	int	TestBit(int bitnum) const;
	bool 	Equals(const Bitmap &other) const;
	bool	Identity(const Bitmap &other) const;

        int	Count(void) const;
private:
	Bits	fBits[NUMWORDS];
};

#endif	/* _BITMAP_H */

//header 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
#include "Bitmap.h"

class BitmapTest : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE (BitmapTest);
    CPPUNIT_TEST (testEquals);
    CPPUNIT_TEST (testSetBit);
    CPPUNIT_TEST (testSetBitAndCount);
    CPPUNIT_TEST (testClearSetAsAndFlip);
    CPPUNIT_TEST (testInvert);
    CPPUNIT_TEST_SUITE_END ();
private:
	Bitmap bitmap1;
	Bitmap bitmap2;
public:
	void setUp();
	void tearDown();
protected:
	void testEquals();
	void testSetBit();
	void testSetBitAndCount();
        void testClearSetAsAndFlip();
        void testInvert();
};
/*
 * File:   BitmapTest.h
 * Author: nabg
 *
 * Created on 26 February 2009, 1:20 PM
 */

#ifndef _BITMAPTEST_H
#define	_BITMAPTEST_H



#endif	/* _BITMAPTEST_H */


//header that want to do testing 


i already implement the cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Bitmap.h"

Bitmap::Bitmap()
{
	Zero();
}

void Bitmap::Zero(void)
{
	for(int i=0;i<NUMWORDS; i++)
		fBits[i] = 0;
}


bool Bitmap::Identity(const Bitmap& other) const
{
	return (this == &other);
}

bool Bitmap::Equals(const Bitmap& other) const
{
	// This is the (hopefully) correct implementation
	for(int i=0;i<NUMWORDS; i++)
		if(fBits[i] != other.fBits[i]) return false;
	return true;
}


void Bitmap::SetBit(int bitnum)
{
	if((bitnum < 0) || (bitnum >= MAXBITS))
		return;
	int word = bitnum / 32;
	int pos = bitnum % 32;

	Bits mask = 1 << pos;
	fBits[word] |= mask;
}

void Bitmap::ClearBit(int bitnum)
{
	if((bitnum < 0) || (bitnum >= MAXBITS))
		return;
	int word = bitnum / 32;
	int pos = bitnum % 32;

	Bits mask = 1 << pos;
	mask = ~mask;

	fBits[word] &= mask;
}

int Bitmap::TestBit(int bitnum) const
{
	if((bitnum < 0) || (bitnum >= MAXBITS))
		return 0;
	int word = bitnum / 32;
	int pos = bitnum % 32;

	Bits mask = 1 << pos;
	return (fBits[word] & mask);
}


int Bitmap::Count(void) const
{
	int count = 0;
	for(int n=0; n < NUMWORDS; n++) {
		Bits x = fBits[n];
		int j = 1;
		for(int i=0;i<32;i++) {
			if(x & j)
				count++;
			j = j << 1;
			}
		}
	return count;
}


my unit testing cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

#include "BitmapTest.h"
#include "Bitmap.h"

CPPUNIT_TEST_SUITE_REGISTRATION (BitmapTest);

void BitmapTest::setUp()
{
	// Put the bitmaps back in standard state
	// (Need at least two as doing things like
	// checking equality)
	bitmap1.Zero();
	bitmap2.Zero();
}

void BitmapTest::tearDown()
{
	// Nothing to do
}

void BitmapTest::testEquals()
{
	// They should be equals now - all zeros
	CPPUNIT_ASSERT(bitmap1.Equals(bitmap2));
	// Set one bit in each - the same bit
	// They should still be equals!
	bitmap1.SetBit(77);
	bitmap2.SetBit(77);
	CPPUNIT_ASSERT(bitmap1.Equals(bitmap2));
	// Set a different bit
	bitmap1.SetBit(22);
	bitmap2.SetBit(123);
	// Shouldn't be equals any more!
	CPPUNIT_ASSERT(!bitmap1.Equals(bitmap2));
	// The two bit maps should be cleaned out
	// prior to next test (by next call to setUp)
        bitmap1.Zero();
        bitmap2.Zero();
        CPPUNIT_ASSERT( bitmap1.Equals(bitmap2));
}

void BitmapTest::testSetBit()
{
	// If set bit 0 in one, they should not be equal
	bitmap1.SetBit(0);
	CPPUNIT_ASSERT(!bitmap1.Equals(bitmap2));
	// Just being pedantic - reverse roles in test
	//CPPUNIT_ASSERT(!bitmap2.Equals(bitmap1));
	bitmap1.Zero();
	// But should be equal after it got cleared
	CPPUNIT_ASSERT(bitmap1.Equals(bitmap2));
	CPPUNIT_ASSERT(bitmap2.Equals(bitmap1));
	// If set a non-existent bit it isn't supposed
	// to change anything
	bitmap1.SetBit(-1);
	bitmap2.SetBit(513);
	CPPUNIT_ASSERT(bitmap2.Equals(bitmap1));
}


void BitmapTest::testSetBitAndCount()
{
	// Set some bits and check that count equals number of bits set
	int bits1[] = { 0, 3, 17, 21, 33, 54, 68, 77, 91, 103, 211, 304 };
	int bits2[] = { 0, 31, 32, 33, 63, 64, 65, 300, 400, 500, 511 };
	int count1 = sizeof(bits1)/sizeof(int);
	int count2 = sizeof(bits2)/sizeof(int);
	for(int i=0;i<count1;i++)
		bitmap1.SetBit(bits1[i]);
	for(int i=0;i<count2;i++)
		bitmap2.SetBit(bits2[i]);
	CPPUNIT_ASSERT_EQUAL(count1, bitmap1.Count());
	// Deliberate error to get a failed test
        count1--;
	 CPPUNIT_ASSERT_EQUAL(count1, bitmap2.Count());
	// Fixed to make it run
	CPPUNIT_ASSERT_EQUAL(count2, bitmap2.Count());
}

void BitmapTest::testClearSetAsAndFlip(){
    //Clear bit . Result same
    bitmap1.ClearBit(-1);
    bitmap2.ClearBit(513);
    CPPUNIT_ASSERT( bitmap1.Equals(bitmap2));
    
    //Set different value of bit, shouldn't be equal anymore!
    bitmap1.SetBit(7);
    bitmap2.SetBit(8);
    CPPUNIT_ASSERT( !bitmap1.Equals(bitmap2));
//    //Clear Bit
    bitmap1.Zero();
    bitmap2.Zero();
    
//    //Clear Bit and result same
    bitmap1.ClearBit(7);
    bitmap1.ClearBit(-512);
    CPPUNIT_ASSERT( bitmap1.Equals(bitmap2));
}

void BitmapTest::testInvert(){
    //bitmap1.SetBit(200);
    //bitmap2.SetBit(250);
}


my question is what kind of code implementation should implement in my function name

 
void testInvert()
i'm sorry, i think your question is really unclear, is that only me, or everyone feel the same?
Topic archived. No new replies allowed.