TEST_F(octtointTest, MaximumUnsigned32bit) { const char* str = "37777777777"; u_long actual; ASSERT_TRUE(octtoint(str, &actual)); EXPECT_EQ(4294967295UL, actual); }
TEST_F(octtointTest, SingleDigit) { const char* str = "5"; u_long actual; ASSERT_TRUE(octtoint(str, &actual)); EXPECT_EQ(5, actual); }
TEST_F(octtointTest, MultipleDigits) { const char* str = "271"; u_long actual; ASSERT_TRUE(octtoint(str, &actual)); EXPECT_EQ(185, actual); }
TEST_F(octtointTest, Zero) { const char* str = "0"; u_long actual; ASSERT_TRUE(octtoint(str, &actual)); EXPECT_EQ(0, actual); }
void test_IllegalCharacter(void){ const char* str = "5ac2"; u_long actual; TEST_ASSERT_FALSE(octtoint(str, &actual) ); }
void test_IllegalDigit(void){ const char* str = "5283"; u_long actual; TEST_ASSERT_FALSE(octtoint(str, &actual) ); }
void test_Overflow(void){ const char* str = "40000000000"; u_long actual; TEST_ASSERT_FALSE(octtoint(str, &actual) ); }
void test_SingleDigit(void) { const char* str = "5"; u_long actual; TEST_ASSERT_TRUE(octtoint(str, &actual) ); TEST_ASSERT_EQUAL(5, actual); }
void test_MaximumUnsigned32bit(void){ const char* str = "37777777777"; u_long actual; TEST_ASSERT_TRUE(octtoint(str, &actual) ); TEST_ASSERT_EQUAL(4294967295UL, actual); }
void test_Zero(void){ const char* str = "0"; u_long actual; TEST_ASSERT_TRUE(octtoint(str, &actual) ); TEST_ASSERT_EQUAL(0, actual); }
void test_MultipleDigits(void){ const char* str = "271"; u_long actual; TEST_ASSERT_TRUE(octtoint(str, &actual) ); TEST_ASSERT_EQUAL(185, actual); }
static PyObject * pyhobdcalc_octdivoct(PyObject *self, PyObject *args) { /** Python function wrapper for dividing two octal integer string and return the result as an float string. **/ char *octdivstr1 ; /** variable for converting an python string into an C string. **/ char *octdivstr2 ; /** variable for converting an python string into an C string. **/ long double retval ; /** Variable for result computing. **/ _Bool is_1_negativ=false ; /** Boolean value to check if the given first argument is negativ. **/ _Bool is_2_negativ=false ; /** Boolean value to check if the given second argument is negativ. **/ _Bool is_result_negativ=false ; /** Boolean value to check if the result should be negativ. **/ _Bool change_argument_value1=false ; /** Variable to check if the octal identifier "0" is present * in the first given argument string. **/ _Bool change_argument_value2=false ; /** Variable to check if the octal identifier "0" is present * in the second given argument string. **/ if (!PyArg_ParseTuple(args, "ss", &octdivstr1,&octdivstr2)) { /** Failing to convert the given python string into an C string. **/ return NULL; } if (octdivstr1[0] == '-' && octdivstr1[1] == '0') { /** The first argument octal string is negativ and start with the octal identifier prefix "0". **/ octdivstr1[1]='-' ; /** We set the negativ sign to index 1. **/ octdivstr1++ ; /** We increment the pointer to point on the negativ sign * so that we get it at the string start. **/ is_1_negativ=true ; /** We set the negativ boolean value on true. **/ change_argument_value1=true ; /** Octal identifier detected with negativ sign. **/ } else if (octdivstr1[0] == '0') { /** The first argument octal string start with the octal identifier prefix "0". **/ octdivstr1++ ; /** We increment the pointer to point on the data begin * to ignore the octal identifier prefix "0". **/ change_argument_value1=true ; /** Octal identifier detected without negativ sign. **/ } else if (octdivstr1[0] == '-') { /** The first argument octal string is negativ. **/ is_1_negativ=true ; /** We set the negativ boolean value on true. **/ } if (octdivstr2[0] == '-' && octdivstr2[1] == '0') { /** The second argument octal string is negativ and start with the octal identifier prefix "0". **/ octdivstr2[1]='-' ; /** We set the negativ sign to index 1. **/ octdivstr2++ ; /** We increment the pointer to point on the negativ sign * so that we get it at the string start. **/ is_2_negativ=true ; /** We set the negativ boolean value on true. **/ change_argument_value2=true ; /** Octal identifier detected with negativ sign. **/ } else if (octdivstr2[0] == '0') { /** The second argument octal string start with the octal identifier prefix "0". **/ octdivstr2++ ; /** We increment the pointer to point on the data begin * to ignore the octal identifier prefix "0". **/ change_argument_value2=true ; /** Octal identifier detected without negativ sign. **/ } else if (octdivstr2[0] == '-') { /** The second argument octal string is negativ. **/ is_2_negativ=true ; /** We set the negativ boolean value on true. **/ } if (strlen(octdivstr1) > 24 && ! is_1_negativ ) { /** The string is too length for the operation peforming. * So we raise an OverflowError exception an abort the division function. **/ PyErr_SetString(PyExc_OverflowError,"Maximal octal string argument 1 length: 21 characters.") ; return NULL ; } else if (strlen(octdivstr1) > 25 && is_1_negativ ) { /** The string is too length for the operation peforming. * So we raise an OverflowError exception an abort the division function. **/ PyErr_SetString(PyExc_OverflowError,"Maximal octal string argument 1 length: 21 characters.") ; return NULL ; } if (strlen(octdivstr2) > 24 && ! is_2_negativ ) { /** The string is too length for the operation peforming. * So we raise an OverflowError exception an abort the division function. **/ PyErr_SetString(PyExc_OverflowError,"Maximal octal string argument 2 length: 21 characters.") ; return NULL ; } else if (strlen(octdivstr2) > 25 && is_2_negativ ) { /** The string is too length for the operation peforming. * So we raise an OverflowError exception an abort the division function. **/ PyErr_SetString(PyExc_OverflowError,"Maximal octal string argument 2 length: 12 characters.") ; return NULL ; } if (! is_string_as_base_an_valid_entry(octdivstr1,8) ) { /** The given string is not an valid octal string. * So we raise an ValueError exception and abort the division function. **/ PyErr_SetString(PyExc_ValueError,"Argument 1 must be an octal string: in form [-][0][0-7].") ; return NULL ; } if (! is_string_as_base_an_valid_entry(octdivstr2,8) ) { /** The given string is not an valid octal string. * So we raise an ValueError exception and abort the division function. **/ PyErr_SetString(PyExc_ValueError,"Argument 2 must be an octal string: in form [-][0][0-7].") ; return NULL ; } if ( is_string_float(octdivstr1) ) { /** The given string is not an valid octal integer string, * but an octal float string. * So we raise an ValueError exception and abort the division function. **/ PyErr_SetString(PyExc_ValueError,"Argument 1 must be an octal integer string: in form [-][0][0-7].") ; return NULL ; } if ( is_string_float(octdivstr2) ) { /** The given string is not an valid octal integer string, * but an octal float string. * So we raise an ValueError exception and abort the division function. **/ PyErr_SetString(PyExc_ValueError,"Argument 2 must be an octal integer string: in form [-][0][0-7].") ; return NULL ; } /** We check if the result should be negativ or positiv. In relationship of the signs and values from the arguments. **/ if ( ( octtoint(octdivstr1) > octtoint(octdivstr2) ) && (! is_1_negativ) && ( is_2_negativ) ) { is_result_negativ=true ; } else if ( ( octtoint(octdivstr1) < octtoint(octdivstr2) ) && is_1_negativ && (! is_2_negativ) ) { is_result_negativ=true ; } else if ( is_1_negativ && is_2_negativ ) { is_result_negativ=false ; } else if ( (! is_1_negativ) && (! is_2_negativ) ) { is_result_negativ=false ; } retval=octdivoct(octdivstr1,octdivstr2) ; /** We perform the division and store the result in retval. **/ if (change_argument_value1 && is_1_negativ) { /** We reset the pointer of the first given argument for giving it correctly back. * In case the user want to reuse the variable given as argument. **/ octdivstr1-- ; octdivstr1[1]='0' ; } else if (change_argument_value1 && (! is_1_negativ) ) { /** We reset the pointer of the first given argument for giving it correctly back. * In case the user want to reuse the variable given as argument. **/ octdivstr1-- ; } if (change_argument_value2 && is_2_negativ) { /** We reset the pointer of the second given argument for giving it correctly back. * In case the user want to reuse the variable given as argument. **/ octdivstr2-- ; octdivstr2[1]='0' ; } else if (change_argument_value2 && (! is_2_negativ) ) { /** We reset the pointer of the second given argument for giving it correctly back. * In case the user want to reuse the variable given as argument. **/ octdivstr2-- ; } char string_to_return[128] ; /** We need an string to return it **/ memset(string_to_return,'\0',128) ; /** Setting all bits on '\0'. **/ sprintf(string_to_return,"%.15Lf",retval) ; /** Copy the result value into an string. **/ strip_zero(string_to_return) ; /** Stripping the uneeded zeros. **/ if ( (string_to_return[0] == '-') && ! is_result_negativ ) { /** The division result is positiv and the result string is negativ. * This come from an value overflow. * So we raise an Overflow exception an abort the operation function. **/ PyErr_SetString(PyExc_OverflowError,"Value overflow: arguments quotient value to great for operation.") ; return NULL ; } else if ( (! string_to_return[0] == '-') && is_result_negativ ) { /** The division result is negativ and the result string is positiv. * This come from an value overflow. * So we raise an OverflowError exception an abort the operation function. **/ PyErr_SetString(PyExc_OverflowError,"Value overflow: arguments quotient value to great for operation.") ; return NULL ; } /** We return the operation result as an string because we cannot return an long double to python. **/ return Py_BuildValue("s",string_to_return); /** Convert an C string into an python string and return it. **/ }
TEST_F(octtointTest, IllegalDigit) { const char* str = "5283"; u_long actual; ASSERT_FALSE(octtoint(str, &actual)); }
TEST_F(octtointTest, IllegalCharacter) { const char* str = "5ac2"; u_long actual; ASSERT_FALSE(octtoint(str, &actual)); }
TEST_F(octtointTest, Overflow) { const char* str = "40000000000"; u_long actual; ASSERT_FALSE(octtoint(str, &actual)); }