aboutsummaryrefslogtreecommitdiff
path: root/pw_tokenizer/ts/printf_decoder_test.ts
blob: 93848482496688c808e0488d84f4e043f9bbe499 (plain)
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
// Copyright 2022 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

/* eslint-env browser */
import { PrintfDecoder } from './printf_decoder';
import IntDB from './int_testdata';

function argFromString(arg: string): Uint8Array {
  const data = new TextEncoder().encode(arg);
  return new Uint8Array([data.length, ...data]);
}

function argFromStringBinary(arg: string): Uint8Array {
  return new Uint8Array(arg.split('').map((ch) => ch.charCodeAt(0)));
}

function argsConcat(...args: Uint8Array[]): Uint8Array {
  let data: number[] = [];
  for (const index in args) {
    const argData = args[index];
    data = data.concat([...argData]);
  }
  return new Uint8Array(data);
}

describe('PrintfDecoder', () => {
  let printfDecoder: PrintfDecoder;

  beforeEach(() => {
    printfDecoder = new PrintfDecoder();
  });

  it('formats string correctly', () => {
    expect(printfDecoder.decode('Hello %s', argFromString('Computer'))).toEqual(
      'Hello Computer',
    );
    expect(
      printfDecoder.decode(
        'Hello %s and %s',
        argsConcat(argFromString('Mac'), argFromString('PC')),
      ),
    ).toEqual('Hello Mac and PC');
  });

  it('formats string + number correctly', () => {
    expect(
      printfDecoder.decode(
        'Hello %s and %u',
        argsConcat(
          argFromString('Computer'),
          argFromStringBinary('\xff\xff\x03'),
        ),
      ),
    ).toEqual('Hello Computer and 4294934528');
  });

  it('formats integers correctly', () => {
    for (let index = 0; index < IntDB.length; index++) {
      const testcase = IntDB[index];
      // Test signed
      expect(
        printfDecoder.decode(testcase[0], argFromStringBinary(testcase[4])),
      ).toEqual(testcase[1]);

      // Test unsigned
      expect(
        printfDecoder.decode(testcase[2], argFromStringBinary(testcase[4])),
      ).toEqual(testcase[3]);
    }
  });

  it('formats string correctly', () => {
    expect(
      printfDecoder.decode(
        'Hello %s and %s',
        argsConcat(argFromString('Mac'), argFromString('PC')),
      ),
    ).toEqual('Hello Mac and PC');
  });

  it('formats varint correctly', () => {
    const arg = argFromStringBinary('\xff\xff\x03');
    expect(printfDecoder.decode('Number %d', arg)).toEqual('Number -32768');
    expect(
      printfDecoder.decode('Numbers %u and %d', argsConcat(arg, arg)),
    ).toEqual('Numbers 4294934528 and -32768');
    expect(printfDecoder.decode('Growth is %u%', arg)).toEqual(
      'Growth is 4294934528%',
    );
  });

  it('formats char correctly', () => {
    expect(
      printfDecoder.decode('Battery: 100%c', argFromStringBinary('\x4a')),
    ).toEqual('Battery: 100%');
    expect(
      printfDecoder.decode('Price: %c97.99', argFromStringBinary('\x48')),
    ).toEqual('Price: $97.99');
  });

  it('formats floats correctly', () => {
    expect(
      printfDecoder.decode(
        'Value: %f',
        argFromStringBinary('\xdb\x0f\x49\x40'),
      ),
    ).toEqual('Value: 3.1415927410125732');
    expect(
      printfDecoder.decode(
        'Value: %.5f',
        argFromStringBinary('\xdb\x0f\x49\x40'),
      ),
    ).toEqual('Value: 3.14159');
  });
});