22// Licensed under the BSD-Clause 2 license.
33// See license.txt file in the project root for full license information.
44
5+ using System ;
56using NUnit . Framework ;
67
78namespace Blake3 . Tests
89{
910 /// <summary>
10- /// Tests for <see cref="Hasher "/>
11+ /// Tests for <see cref="Blake3HashAlgorithm "/>
1112 /// </summary>
1213 public class Blake3HashAlgorithmTests : Blake3TestsBase
1314 {
1415 [ Test ]
1516 public void TestComputeHash ( )
1617 {
17- var hashAlgorithm = new Blake3HashAlgorithm ( ) ;
18+ using var hashAlgorithm = new Blake3HashAlgorithm ( ) ;
1819 var result = hashAlgorithm . ComputeHash ( HasherTests . BigData ) ;
1920 var hash = Hash . FromBytes ( result ) ;
2021 AssertTextAreEqual ( HasherTests . BigExpected , hash . ToString ( ) ) ;
2122 }
23+
24+ [ Test ]
25+ public void TestComputeHash_Span_And_TryHashFinal ( )
26+ {
27+ using var hashAlgorithm = new Blake3HashAlgorithm ( ) ;
28+
29+ var data = HasherTests . BigData ;
30+
31+ Span < byte > destination = stackalloc byte [ Hash . Size ] ;
32+ Assert . True ( hashAlgorithm . TryComputeHash ( data , destination , out var written ) ) ;
33+ Assert . AreEqual ( Hash . Size , written ) ;
34+
35+ var hash = Hash . FromBytes ( destination ) ;
36+ AssertTextAreEqual ( HasherTests . BigExpected , hash . ToString ( ) ) ;
37+ }
38+
39+ [ Test ]
40+ public void TestTryHashFinal_BufferTooSmall ( )
41+ {
42+ using var hashAlgorithm = new Blake3HashAlgorithm ( ) ;
43+
44+ Span < byte > destination = stackalloc byte [ Hash . Size - 1 ] ;
45+ Assert . False ( hashAlgorithm . TryComputeHash ( HasherTests . BigData , destination , out var written ) ) ;
46+ Assert . AreEqual ( 0 , written ) ;
47+ }
48+
49+ [ Test ]
50+ public void TestChunking_And_InitializeReuse ( )
51+ {
52+ var data = HasherTests . BigData ;
53+
54+ using var hashAlgorithm = new Blake3HashAlgorithm ( ) ;
55+
56+ // Chunked updates
57+ hashAlgorithm . TransformBlock ( data , 0 , 7 , null , 0 ) ;
58+ hashAlgorithm . TransformBlock ( data , 7 , 13 , null , 0 ) ;
59+ hashAlgorithm . TransformBlock ( data , 20 , data . Length - 20 , null , 0 ) ;
60+ hashAlgorithm . TransformFinalBlock ( Array . Empty < byte > ( ) , 0 , 0 ) ;
61+
62+ var hash1 = Hash . FromBytes ( hashAlgorithm . Hash ) ;
63+ AssertTextAreEqual ( HasherTests . BigExpected , hash1 . ToString ( ) ) ;
64+
65+ // Reuse
66+ hashAlgorithm . Initialize ( ) ;
67+ var result = hashAlgorithm . ComputeHash ( data ) ;
68+ var hash2 = Hash . FromBytes ( result ) ;
69+ AssertTextAreEqual ( HasherTests . BigExpected , hash2 . ToString ( ) ) ;
70+ }
2271 }
2372}
0 commit comments