Browse Source

bug fix: the behaviour of the function power() for 0 to the power of 0 and and for an exponent with the value 0 for any positive base in the range of the data type is not undefined anymore.

remotes/origin/development
fdai7057 2 years ago
parent
commit
b8c357c667
  1. 4
      src/stringManipulation.c
  2. 2
      test/test_StringManipulation.c

4
src/stringManipulation.c

@ -55,6 +55,9 @@ char *to_string(int number)
} }
unsigned int power(unsigned int b, unsigned int n){ unsigned int power(unsigned int b, unsigned int n){
if(b==0&&n==0) return 0;
else if(b>=1&&n==0) return 1;
else{
unsigned int result = 1, ctr = 0; unsigned int result = 1, ctr = 0;
while(ctr<n){ while(ctr<n){
result *= b; result *= b;
@ -62,6 +65,7 @@ unsigned int power(unsigned int b, unsigned int n){
} }
return result; return result;
} }
}
unsigned int toUnsignedInteger(char *ID) unsigned int toUnsignedInteger(char *ID)
{ {

2
test/test_StringManipulation.c

@ -43,7 +43,7 @@ void test_power()
/*test block 5*/ /*test block 5*/
int testValues_5[] = {0,0,19,2,4,5,11,54,32,12,77}; int testValues_5[] = {0,0,19,2,4,5,11,54,32,12,77};
int exponents[] = {0,1,2,7,4,2,0,1,2,4,2}; int exponents[] = {0,1,2,7,4,2,0,1,2,4,2};
int expectedValues_5[] = {-1, 1, 361,128,256,25,1,54,1024,20736,5929};
int expectedValues_5[] = {0, 0, 361,128,256,25,1,54,1024,20736,5929};
length = sizeof(testValues_5)/sizeof(int); length = sizeof(testValues_5)/sizeof(int);
for(int i=0;i<length;++i){ for(int i=0;i<length;++i){
TEST_ASSERT_EQUAL_INT(expectedValues_5[i], power(testValues_5[i],exponents[i])); TEST_ASSERT_EQUAL_INT(expectedValues_5[i], power(testValues_5[i],exponents[i]));

Loading…
Cancel
Save