github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/model/sampling/resample_test.go (about) 1 /* 2 * This file is subject to the terms and conditions defined in 3 * file 'LICENSE.md', which is part of this source code package. 4 */ 5 6 package sampling 7 8 import ( 9 "fmt" 10 "testing" 11 ) 12 13 type TestResamplingTest1 struct { 14 InputData []byte 15 BitsPerSample int 16 Expected []uint32 17 } 18 19 func samplesEqual(d1 []uint32, d2 []uint32) bool { 20 if len(d1) != len(d2) { 21 return false 22 } 23 24 for i := 0; i < len(d1); i++ { 25 if d1[i] != d2[i] { 26 return false 27 } 28 } 29 30 return true 31 } 32 33 // Test resampling example data with different bits per sample. 34 // Input is in bytes. 35 func TestResamplingBytes(t *testing.T) { 36 // 0xB5 0x5D 0x2A 37 // 10110101 01011101 00101010 38 // 2-bit resampling: 39 // 10 11 01 01 01 01 11 01 00 10 10 10 40 // 2 3 1 1 1 1 3 1 0 2 2 2 41 // 3-bit resampling: 42 // 101 101 010 101 110 100 101 010 43 // 5 5 2 5 6 4 5 2 44 // 4-bit resampling: 45 // 1011 0101 0101 1101 0010 1010 46 // 11 5 5 13 2 10 47 // 5-bit resampling 48 // 10110 10101 01110 10010 (1010)<- the remainder is dumped 49 // 22 21 14 18 50 // 12-bit resampling 51 // 101101010101 110100101010 52 // 2901 3370 53 // 13-bit resampling 54 // 1011010101011 (10100101010) 55 // 16-bit resampling 56 // 1011010101011101 (00101010) 57 // 46429 58 // 24-bit resampling 59 // 101101010101110100101010 60 // 61 // 0xde 0xad 0xbe 0xef 0x15 0x13 0x37 62 63 testcases := []TestResamplingTest1{ 64 {[]byte{0xB5, 0x5D, 0x2A}, 1, []uint32{1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0}}, 65 {[]byte{0xB5, 0x5D, 0x2A}, 2, []uint32{2, 3, 1, 1, 1, 1, 3, 1, 0, 2, 2, 2}}, 66 {[]byte{0xB5, 0x5D, 0x2A}, 3, []uint32{5, 5, 2, 5, 6, 4, 5, 2}}, 67 {[]byte{0xB5, 0x5D, 0x2A}, 4, []uint32{11, 5, 5, 13, 2, 10}}, 68 {[]byte{0xB5, 0x5D, 0x2A}, 5, []uint32{22, 21, 14, 18}}, 69 {[]byte{0xB5, 0x5D, 0x2A}, 8, []uint32{0xB5, 0x5D, 0x2A}}, 70 {[]byte{0xB5, 0x5D, 0x2A}, 12, []uint32{2901, 3370}}, 71 {[]byte{0xB5, 0x5D, 0x2A}, 13, []uint32{5803}}, 72 {[]byte{0xB5, 0x5D, 0x2A}, 16, []uint32{0xB55D}}, 73 {[]byte{0xB5, 0x5D, 0x2A}, 24, []uint32{0xB55D2A}}, 74 {[]byte{0xde, 0xad, 0xbe, 0xef, 0x15, 0x13, 0x37, 0x20}, 24, []uint32{0xdeadbe, 0xef1513}}, 75 {[]byte{0xde, 0xad, 0xbe, 0xef, 0x15, 0x13, 0x37, 0x20, 0x21}, 32, []uint32{0xdeadbeef, 0x15133720}}, 76 } 77 78 for _, testcase := range testcases { 79 b := ResampleBytes(testcase.InputData, testcase.BitsPerSample) 80 fmt.Println(b) 81 if !samplesEqual(b, testcase.Expected) { 82 t.Errorf("Test case failed. Got: % d, expected: % d", b, testcase.Expected) 83 t.Errorf("Test case failed. Got: % X, expected: % X", b, testcase.Expected) 84 } 85 } 86 } 87 88 type TestResamplingTest2 struct { 89 InputData []uint32 90 BitsPerInputSample int 91 BitsPerOutputSample int 92 Expected []uint32 93 } 94 95 // Test resampling example data with different bits per sample. 96 // Input is in uint32. 97 func TestResamplingUint32(t *testing.T) { 98 // 0xB5 0x5D 0x2A 0x00 99 // 10110101 01011101 00101010 00000000 100 // 2-bit resampling: 101 // 10 11 01 01 01 01 11 01 00 10 10 10 00 00 00 00 102 // 2 3 1 1 1 1 3 1 0 2 2 2 0 0 0 0 103 // 3-bit resampling: 104 // 101 101 010 101 110 100 101 010 000 000 (00) 105 // 5 5 2 5 6 4 5 2 0 0 106 // 4-bit resampling: 107 // 1011 0101 0101 1101 0010 1010 0000 0000 108 // 11 5 5 13 2 10 0 0 109 // 5-bit resampling 110 // 10110 10101 01110 10010 10100 00000 (00)<- the remainder is dumped 111 // 22 21 14 18 20 0 112 // 12-bit resampling 113 // 101101010101 110100101010 (00000000) 114 // 2901 3370 115 // 13-bit resampling 116 // 1011010101011 1010010101000 (0000000) 117 // 16-bit resampling 118 // 1011010101011101 0010101000000000 119 // 0xB55D 0x2A00 120 // 24-bit resampling 121 // 101101010101110100101010 (00000000) 122 // 123 // 0xde 0xad 0xbe 0xef 0x15 0x13 0x37 124 125 testcases := []TestResamplingTest2{ 126 {[]uint32{0xB55D2A00}, 32, 1, []uint32{1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, 127 {[]uint32{0xB55D2A00}, 32, 2, []uint32{2, 3, 1, 1, 1, 1, 3, 1, 0, 2, 2, 2, 0, 0, 0, 0}}, 128 {[]uint32{0xB55D2A00}, 32, 3, []uint32{5, 5, 2, 5, 6, 4, 5, 2, 0, 0}}, 129 {[]uint32{0xB55D2A00}, 32, 4, []uint32{11, 5, 5, 13, 2, 10, 0, 0}}, 130 {[]uint32{0xB55D2A00}, 32, 5, []uint32{22, 21, 14, 18, 20, 0}}, 131 {[]uint32{0xB55D2A00}, 32, 8, []uint32{0xB5, 0x5D, 0x2A, 0x00}}, 132 {[]uint32{0xB55D2A00}, 32, 12, []uint32{2901, 3370}}, 133 {[]uint32{0xB55D2A00}, 32, 13, []uint32{5803, 5288}}, 134 {[]uint32{0xB55D2A00}, 32, 16, []uint32{0xB55D, 0x2A00}}, 135 {[]uint32{0xB55D2A00}, 32, 24, []uint32{0xB55D2A}}, 136 {[]uint32{0xdeadbeef, 0x15133720}, 32, 24, []uint32{0xdeadbe, 0xef1513}}, 137 {[]uint32{0xdeadbeef, 0x15133720}, 32, 32, []uint32{0xdeadbeef, 0x15133720}}, 138 } 139 140 for _, testcase := range testcases { 141 b := ResampleUint32(testcase.InputData, testcase.BitsPerInputSample, testcase.BitsPerOutputSample) 142 fmt.Println(b) 143 if !samplesEqual(b, testcase.Expected) { 144 //t.Errorf("Test case failed. Got: % d, expected: % d", b, testcase.Expected) 145 t.Errorf("Test case failed. Got: % X, expected: % X", b, testcase.Expected) 146 } 147 } 148 } 149 150 // Test resampling example data with different bits per sample. 151 // Input is in uint32, certain number of bits. 152 func TestResamplingUint32xx(t *testing.T) { 153 testcases := []TestResamplingTest2{ 154 {[]uint32{0, 0, 0}, 1, 8, []uint32{0}}, 155 {[]uint32{0, 1, 0}, 1, 8, []uint32{64}}, 156 } 157 158 for _, testcase := range testcases { 159 b := ResampleUint32(testcase.InputData, testcase.BitsPerInputSample, testcase.BitsPerOutputSample) 160 fmt.Println(b) 161 if !samplesEqual(b, testcase.Expected) { 162 t.Errorf("Test case failed. Got: % d, expected: % d", b, testcase.Expected) 163 t.Errorf("Test case failed. Got: % X, expected: % X", b, testcase.Expected) 164 } 165 } 166 }