|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, AcmeStack |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package ascii85x |
| 19 | + |
| 20 | +import "testing" |
| 21 | + |
| 22 | +var pairs = []struct { |
| 23 | + decoded string |
| 24 | + encoded string |
| 25 | +}{ |
| 26 | + { |
| 27 | + "", |
| 28 | + "", |
| 29 | + }, |
| 30 | + { |
| 31 | + "\000\000\000\000", |
| 32 | + "z", |
| 33 | + }, |
| 34 | +} |
| 35 | + |
| 36 | +func TestAscii85EncodeToInt(t *testing.T) { |
| 37 | + for _, p := range pairs { |
| 38 | + buf := make([]byte, Ascii85MaxEncodedLenToInt(len(p.decoded))) |
| 39 | + result := Ascii85EncodeToInt(buf, []byte(p.decoded)) |
| 40 | + buf = buf[0:result] |
| 41 | + if strip85(string(buf)) != strip85(p.encoded) { |
| 42 | + t.Errorf("TestAscii85EncodeToInt() result = %v, want %v", result, strip85(p.encoded)) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestAscii85Decode(t *testing.T) { |
| 48 | + for _, p := range pairs { |
| 49 | + dbuf := make([]byte, 4*len(p.encoded)) |
| 50 | + ndst, nsrc, err := Ascii85Decode(dbuf, []byte(p.encoded), true) |
| 51 | + if err != error(nil) { |
| 52 | + t.Errorf("TestAscii85Decode() error = %v, want %v", err, error(nil)) |
| 53 | + } |
| 54 | + if nsrc != len(p.encoded) { |
| 55 | + t.Errorf("TestAscii85Decode() nsrc = %v, want %v", nsrc, len(p.encoded)) |
| 56 | + } |
| 57 | + if ndst != len(p.decoded) { |
| 58 | + t.Errorf("TestAscii85Decode() ndst = %v, want %v", ndst, len(p.decoded)) |
| 59 | + } |
| 60 | + if string(dbuf[0:ndst]) != p.decoded { |
| 61 | + t.Errorf("TestAscii85Decode() want %v", p.decoded) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func strip85(s string) string { |
| 67 | + t := make([]byte, len(s)) |
| 68 | + w := 0 |
| 69 | + for r := 0; r < len(s); r++ { |
| 70 | + c := s[r] |
| 71 | + if c > ' ' { |
| 72 | + t[w] = c |
| 73 | + w++ |
| 74 | + } |
| 75 | + } |
| 76 | + return string(t[0:w]) |
| 77 | +} |
0 commit comments