Skip to content

Commit 6ebc761

Browse files
committed
auto merge of #5140 : yjh0502/rust/issue_4458, r=catamorphism
Fix is a bug fix for issue #4458. This patch is quite straight-forward. A test for result_str() is added.
2 parents 33e7a1f + 5ae9b29 commit 6ebc761

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/libstd/sha1.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ pub fn sha1() -> Sha1 {
251251
let rr = mk_result(self);
252252
let mut s = ~"";
253253
for vec::each(rr) |b| {
254-
s += uint::to_str_radix(*b as uint, 16u);
254+
let hex = uint::to_str_radix(*b as uint, 16u);
255+
if hex.len() == 1 {
256+
s += "0";
257+
}
258+
s += hex;
255259
}
256260
return s;
257261
}
@@ -283,6 +287,7 @@ mod tests {
283287
struct Test {
284288
input: ~str,
285289
output: ~[u8],
290+
output_str: ~str,
286291
}
287292

288293
fn a_million_letter_a() -> ~str {
@@ -306,6 +311,7 @@ mod tests {
306311
0x78u8, 0x50u8, 0xC2u8, 0x6Cu8,
307312
0x9Cu8, 0xD0u8, 0xD8u8, 0x9Du8,
308313
],
314+
output_str: ~"a9993e364706816aba3e25717850c26c9cd0d89d"
309315
},
310316
Test {
311317
input:
@@ -318,6 +324,7 @@ mod tests {
318324
0xF9u8, 0x51u8, 0x29u8, 0xE5u8,
319325
0xE5u8, 0x46u8, 0x70u8, 0xF1u8,
320326
],
327+
output_str: ~"84983e441c3bd26ebaae4aa1f95129e5e54670f1"
321328
},
322329
Test {
323330
input: a_million_letter_a(),
@@ -328,6 +335,7 @@ mod tests {
328335
0xDBu8, 0xADu8, 0x27u8, 0x31u8,
329336
0x65u8, 0x34u8, 0x01u8, 0x6Fu8,
330337
],
338+
output_str: ~"34aa973cd4c4daa4f61eeb2bdbad27316534016f"
331339
},
332340
];
333341
// Examples from wikipedia
@@ -342,6 +350,7 @@ mod tests {
342350
0xbbu8, 0x76u8, 0xe7u8, 0x39u8,
343351
0x1bu8, 0x93u8, 0xebu8, 0x12u8,
344352
],
353+
output_str: ~"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
345354
},
346355
Test {
347356
input: ~"The quick brown fox jumps over the lazy cog",
@@ -352,6 +361,7 @@ mod tests {
352361
0x0bu8, 0xd1u8, 0x7du8, 0x9bu8,
353362
0x10u8, 0x0du8, 0xb4u8, 0xb3u8,
354363
],
364+
output_str: ~"de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3",
355365
},
356366
];
357367
let tests = fips_180_1_tests + wikipedia_tests;
@@ -373,6 +383,11 @@ mod tests {
373383
sh.input_str(t.input);
374384
let out = sh.result();
375385
check_vec_eq(t.output, out);
386+
387+
let out_str = sh.result_str();
388+
assert(out_str.len() == 40);
389+
assert(out_str == t.output_str);
390+
376391
sh.reset();
377392
}
378393

@@ -389,6 +404,11 @@ mod tests {
389404
}
390405
let out = sh.result();
391406
check_vec_eq(t.output, out);
407+
408+
let out_str = sh.result_str();
409+
assert(out_str.len() == 40);
410+
assert(out_str == t.output_str);
411+
392412
sh.reset();
393413
}
394414
}

0 commit comments

Comments
 (0)