From 2d0e7cd272136f12f8c07cafe57df408d37ada6f Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 4 Jun 2012 15:22:40 -0700 Subject: [PATCH] core: Don't allow radix 1 in uint::to_str --- src/libcore/uint-template.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index 0904a6d9382..6e513b2f5e2 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -95,9 +95,15 @@ fn from_str_radix(buf: str, radix: u64) -> option { }; } -#[doc = "Convert to a string in a given base"] +#[doc = " +Convert to a string in a given base + +# Failure + +Fails if `radix` < 2 or `radix` > 16 +"] fn to_str(num: T, radix: uint) -> str { - assert (0u < radix && radix <= 16u); + assert (1u < radix && radix <= 16u); let mut n = num; let radix = radix as T; fn digit(n: T) -> u8 { @@ -167,3 +173,17 @@ fn test_parse_buf() { assert parse_buf(str::bytes("Z"), 10u) == none; assert parse_buf(str::bytes("_"), 2u) == none; } + +#[test] +#[should_fail] +#[ignore(cfg(target_os = "win32"))] +fn to_str_radix1() { + uint::to_str(100u, 1u); +} + +#[test] +#[should_fail] +#[ignore(cfg(target_os = "win32"))] +fn to_str_radix17() { + uint::to_str(100u, 17u); +}