use prefix(0x and 0b) if it conflicts with base

dev
MITSUNARI Shigeo 8 years ago
parent 72ef710ee4
commit f79260f5aa
  1. 9
      src/fp.cpp
  2. 11
      test/fp_test.cpp
  3. 11
      test/mont_fp_test.cpp

@ -48,6 +48,9 @@ void Op::destroyFpGenerator(FpGenerator *)
}
#endif
/*
use prefix if base conflicts with prefix
*/
inline const char *verifyStr(bool *isMinus, int *base, const std::string& str)
{
const char *p = str.c_str();
@ -59,15 +62,9 @@ inline const char *verifyStr(bool *isMinus, int *base, const std::string& str)
}
if (p[0] == '0') {
if (p[1] == 'x') {
if (*base != 0 && *base != 16) {
throw cybozu::Exception("fp:verifyStr:bad base") << *base << str;
}
*base = 16;
p += 2;
} else if (p[1] == 'b') {
if (*base != 0 && *base != 2) {
throw cybozu::Exception("fp:verifyStr:bad base") << *base << str;
}
*base = 2;
p += 2;
}

@ -85,16 +85,15 @@ CYBOZU_TEST_AUTO(setStr)
{ "0b100", 4, 2 },
{ "0x100", 256, 0 },
{ "0x100", 256, 16 },
// use prefix if base conflicts with prefix
{ "0b100", 4, 16 },
{ "0x100", 256, 2 },
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
Fp x;
x.setStr(tbl[i].in, tbl[i].base);
CYBOZU_TEST_EQUAL(x, tbl[i].out);
}
// conflict prefix with base
Fp x;
CYBOZU_TEST_EXCEPTION(x.setStr("0b100", 16), cybozu::Exception);
CYBOZU_TEST_EXCEPTION(x.setStr("0x100", 2), cybozu::Exception);
}
CYBOZU_TEST_AUTO(stream)
@ -121,9 +120,11 @@ CYBOZU_TEST_AUTO(stream)
CYBOZU_TEST_EQUAL(x, tbl[i].out16);
}
}
// use prefix if base conflicts with prefix
std::istringstream is("0b100");
Fp x;
CYBOZU_TEST_EXCEPTION(is >> std::hex >> x, cybozu::Exception);
is >> std::hex >> x;
CYBOZU_TEST_EQUAL(x, 4);
{
std::ostringstream os;
os << Fp(123);

@ -248,16 +248,15 @@ struct Test {
{ "0b100", 4, 2 },
{ "0x100", 256, 0 },
{ "0x100", 256, 16 },
// use prefix if base conflicts with prefix
{ "0b100", 4, 16 },
{ "0x100", 256, 2 },
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
Fp x;
x.setStr(tbl[i].in, tbl[i].base);
CYBOZU_TEST_EQUAL(x, tbl[i].out);
}
// conflict prefix with base
Fp x;
CYBOZU_TEST_EXCEPTION(x.setStr("0b100", 16), cybozu::Exception);
CYBOZU_TEST_EXCEPTION(x.setStr("0x100", 2), cybozu::Exception);
}
void stream()
@ -286,7 +285,9 @@ struct Test {
}
std::istringstream is("0b100");
Fp x;
CYBOZU_TEST_EXCEPTION(is >> std::hex >> x, cybozu::Exception);
// use prefix if base conflicts with prefix
is >> std::hex >> x;
CYBOZU_TEST_EQUAL(x, 4);
}
void ioMode()
{

Loading…
Cancel
Save