1// Copyright 2022 The Pigweed Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not 4// use this file except in compliance with the License. You may obtain a copy of 5// the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12// License for the specific language governing permissions and limitations under 13// the License. 14 15/* eslint-env browser */ 16import { PrintfDecoder } from './printf_decoder'; 17import IntDB from './int_testdata'; 18 19function argFromString(arg: string): Uint8Array { 20 const data = new TextEncoder().encode(arg); 21 return new Uint8Array([data.length, ...data]); 22} 23 24function argFromStringBinary(arg: string): Uint8Array { 25 return new Uint8Array(arg.split('').map((ch) => ch.charCodeAt(0))); 26} 27 28function argsConcat(...args: Uint8Array[]): Uint8Array { 29 let data: number[] = []; 30 for (const index in args) { 31 const argData = args[index]; 32 data = data.concat([...argData]); 33 } 34 return new Uint8Array(data); 35} 36 37describe('PrintfDecoder', () => { 38 let printfDecoder: PrintfDecoder; 39 40 beforeEach(() => { 41 printfDecoder = new PrintfDecoder(); 42 }); 43 44 it('formats string correctly', () => { 45 expect(printfDecoder.decode('Hello %s', argFromString('Computer'))).toEqual( 46 'Hello Computer', 47 ); 48 expect( 49 printfDecoder.decode( 50 'Hello %s and %s', 51 argsConcat(argFromString('Mac'), argFromString('PC')), 52 ), 53 ).toEqual('Hello Mac and PC'); 54 }); 55 56 it('formats string + number correctly', () => { 57 expect( 58 printfDecoder.decode( 59 'Hello %s and %u', 60 argsConcat( 61 argFromString('Computer'), 62 argFromStringBinary('\xff\xff\x03'), 63 ), 64 ), 65 ).toEqual('Hello Computer and 4294934528'); 66 }); 67 68 it('formats integers correctly', () => { 69 for (let index = 0; index < IntDB.length; index++) { 70 const testcase = IntDB[index]; 71 // Test signed 72 expect( 73 printfDecoder.decode(testcase[0], argFromStringBinary(testcase[4])), 74 ).toEqual(testcase[1]); 75 76 // Test unsigned 77 expect( 78 printfDecoder.decode(testcase[2], argFromStringBinary(testcase[4])), 79 ).toEqual(testcase[3]); 80 } 81 }); 82 83 it('formats string correctly', () => { 84 expect( 85 printfDecoder.decode( 86 'Hello %s and %s', 87 argsConcat(argFromString('Mac'), argFromString('PC')), 88 ), 89 ).toEqual('Hello Mac and PC'); 90 }); 91 92 it('formats varint correctly', () => { 93 const arg = argFromStringBinary('\xff\xff\x03'); 94 expect(printfDecoder.decode('Number %d', arg)).toEqual('Number -32768'); 95 expect( 96 printfDecoder.decode('Numbers %u and %d', argsConcat(arg, arg)), 97 ).toEqual('Numbers 4294934528 and -32768'); 98 expect(printfDecoder.decode('Growth is %u%', arg)).toEqual( 99 'Growth is 4294934528%', 100 ); 101 }); 102 103 it('formats char correctly', () => { 104 expect( 105 printfDecoder.decode('Battery: 100%c', argFromStringBinary('\x4a')), 106 ).toEqual('Battery: 100%'); 107 expect( 108 printfDecoder.decode('Price: %c97.99', argFromStringBinary('\x48')), 109 ).toEqual('Price: $97.99'); 110 }); 111 112 it('formats floats correctly', () => { 113 expect( 114 printfDecoder.decode( 115 'Value: %f', 116 argFromStringBinary('\xdb\x0f\x49\x40'), 117 ), 118 ).toEqual('Value: 3.1415927410125732'); 119 expect( 120 printfDecoder.decode( 121 'Value: %.5f', 122 argFromStringBinary('\xdb\x0f\x49\x40'), 123 ), 124 ).toEqual('Value: 3.14159'); 125 }); 126}); 127