Skip to content

Commit c4142f2

Browse files
brsongraydon
authored andcommitted
---
yaml --- r: 1630 b: refs/heads/master c: 922f693 h: refs/heads/master v: v3
1 parent c9f3cde commit c4142f2

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: ac72f42b1c103c670a78056d359eff85bf518fae
2+
refs/heads/master: 922f69387dab9f751ab110055d5b9638dbae6a78

trunk/src/lib/_int.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ fn to_str(mutable int n, uint radix) -> str
3434
}
3535
}
3636
37+
fn pow(int base, uint exponent) -> int {
38+
39+
if (exponent == 0u) {
40+
ret 1;
41+
} else if (base == 0) {
42+
ret 0;
43+
} else {
44+
auto accum = base;
45+
auto count = exponent;
46+
while (count > 1u) {
47+
accum *= base;
48+
count -= 1u;
49+
}
50+
ret accum;
51+
}
52+
}
53+
3754
// Local Variables:
3855
// mode: rust;
3956
// fill-column: 78;

trunk/src/test/run-pass/lib-int.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ fn test_to_str() {
1111
check (eq(_int.to_str(100, 10u), "100"));
1212
}
1313

14+
fn test_pow() {
15+
check (_int.pow(0, 0u) == 1);
16+
check (_int.pow(0, 1u) == 0);
17+
check (_int.pow(0, 2u) == 0);
18+
check (_int.pow(-1, 0u) == -1);
19+
check (_int.pow(1, 0u) == 1);
20+
check (_int.pow(-3, 2u) == 9);
21+
check (_int.pow(-3, 3u) == -27);
22+
check (_int.pow(4, 9u) == 262144);
23+
}
24+
1425
fn main() {
1526
test_to_str();
1627
}

0 commit comments

Comments
 (0)