File: ByteVectorTest.cs

package info (click to toggle)
taglib-sharp 2.0.3.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,120 kB
  • ctags: 3,330
  • sloc: cs: 26,081; sh: 3,439; makefile: 283
file content (155 lines) | stat: -rw-r--r-- 5,323 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Security.Cryptography;
using NUnit.Framework;
using TagLib;

namespace TagLib.Tests.Collections
{   
    [TestFixture]
    public class ByteVectorTest
    {   
        private static readonly string TestInput = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static readonly ByteVector TestVector = ByteVector.FromString(TestInput, StringType.UTF8);
        
        [Test]
        public void Length()
        {
            Assert.AreEqual(TestInput.Length, TestVector.Count);
        }
        
        [Test]
        public void StartsWith()
        {
            Assert.IsTrue(TestVector.StartsWith("ABCDE"));
            Assert.IsFalse(TestVector.StartsWith("NOOP"));
        }
        
        [Test]
        public void EndsWith()
        {
            Assert.IsTrue(TestVector.EndsWith("UVWXYZ"));
            Assert.IsFalse(TestVector.EndsWith("NOOP"));
        }
        
        [Test]
        public void ContainsAt()
        {
            Assert.IsTrue(TestVector.ContainsAt("JKLMNO", 9));
            Assert.IsFalse(TestVector.ContainsAt("NOOP", 30));
        }
        
        [Test]
        public void Find()
        {
            Assert.AreEqual(17, TestVector.Find("RSTUV"));
            Assert.AreEqual(-1, TestVector.Find("NOOP"));
        }
    
        [Test]
        public void RFind()
        {
            Assert.AreEqual(6, TestVector.RFind("GHIJ"));
            Assert.AreEqual(-1, TestVector.RFind("NOOP"));
        }
    
        [Test]
        public void Mid()
        {
            Assert.AreEqual(ByteVector.FromString("KLMNOPQRSTUVWXYZ", StringType.UTF8), TestVector.Mid(10));
            Assert.AreEqual(ByteVector.FromString("PQRSTU", StringType.UTF8), TestVector.Mid(15, 6));
        }
    
        [Test]
        public void CopyResize()
        {
            ByteVector a = new ByteVector(TestVector);
            ByteVector b = ByteVector.FromString("ABCDEFGHIJKL", StringType.UTF8);
            a.Resize(12);
            
            Assert.AreEqual(b, a);
            Assert.AreEqual( b.ToString(), a.ToString());
            Assert.IsFalse(a.Count == TestVector.Count);
        }
        
        [Test]
        public void UInt()
        {
            Assert.AreEqual(UInt32.MaxValue, ByteVector.FromUInt(UInt32.MaxValue).ToUInt());
            Assert.AreEqual(UInt32.MinValue, ByteVector.FromUInt(UInt32.MinValue).ToUInt());
            Assert.AreEqual(0, ByteVector.FromUInt(0).ToUInt());
            Assert.AreEqual(30292, ByteVector.FromUInt(30292).ToUInt());
        }
        
        [Test]
        public void Long()
        {
            Assert.AreEqual(UInt64.MaxValue, ByteVector.FromULong(UInt64.MaxValue).ToULong());
            Assert.AreEqual(UInt64.MinValue, ByteVector.FromULong(UInt64.MinValue).ToULong());
            Assert.AreEqual(0, ByteVector.FromULong(0).ToULong());
            Assert.AreEqual(30292, ByteVector.FromULong(30292).ToULong());
        }
        
        [Test]
        public void Short()
        {
            Assert.AreEqual(UInt16.MaxValue, ByteVector.FromUShort(UInt16.MaxValue).ToUShort());
            Assert.AreEqual(UInt16.MinValue, ByteVector.FromUShort(UInt16.MinValue).ToUShort());
            Assert.AreEqual(0, ByteVector.FromUShort(0).ToUShort());
            Assert.AreEqual(8009, ByteVector.FromUShort(8009).ToUShort());
        }
        
        [Test]
        public void FromUri()
        {
            ByteVector vector = ByteVector.FromPath("samples/vector.bin");
            Assert.AreEqual(3282169185, vector.Checksum);
            Assert.AreEqual("1aaa46c484d70c7c80510a5f99e7805d", MD5Hash(vector.Data));
        }
        
        [Test]
        [Ignore("Skip performance testing")]
        public void OperatorAdd()
        {
            using(new CodeTimer("Operator Add")) {
                ByteVector vector = new ByteVector();
                for(int i = 0; i < 10000; i++) {
                    vector += ByteVector.FromULong((ulong)55);
                }
            }
            
            using(new CodeTimer("Function Add")) {
                ByteVector vector = new ByteVector();
                for(int i = 0; i < 10000; i++) {
                    vector.Add(ByteVector.FromULong((ulong)55));
                }
            }
        }
        
        [Test]
        public void CommentsFrameError ()
        {
            // http://bugzilla.gnome.org/show_bug.cgi?id=582735
            // Comments data found in the wild
            ByteVector vector = new ByteVector (
                1, 255, 254, 73, 0, 68, 0, 51, 0, 71, 0, 58, 0, 32, 0, 50, 0, 55, 0, 0, 0);

            var encoding = (StringType) vector [0];
            var language = vector.ToString (StringType.Latin1, 1, 3);
            var split = vector.ToStrings (encoding, 4, 3);
            Assert.AreEqual (2, split.Length);
        }

        private static string MD5Hash(byte [] bytes)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte [] hash_bytes = md5.ComputeHash(bytes);
            string hash_string = String.Empty;

            for(int i = 0; i < hash_bytes.Length; i++) {
                hash_string += Convert.ToString(hash_bytes[i], 16).PadLeft(2, '0');
            }

            return hash_string.PadLeft(32, '0');
        }
    }
}