github.com/gagliardetto/solana-go@v1.11.0/programs/compute-budget/instruction_test.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://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,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package computebudget
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/hex"
    20  	"testing"
    21  
    22  	bin "github.com/gagliardetto/binary"
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestEncodingInstruction(t *testing.T) {
    28  	tests := []struct {
    29  		name              string
    30  		hexData           string
    31  		expectInstruction *Instruction
    32  	}{
    33  		{
    34  			name:    "RequestUnitsDeprecated",
    35  			hexData: "00c05c1500e8030000",
    36  			expectInstruction: &Instruction{
    37  				BaseVariant: bin.BaseVariant{
    38  					TypeID: bin.TypeIDFromUint8(0),
    39  					Impl: &RequestUnitsDeprecated{
    40  						Units:         1400000,
    41  						AdditionalFee: 1000,
    42  					},
    43  				},
    44  			},
    45  		},
    46  		{
    47  			name:    "RequestHeapFrame",
    48  			hexData: "01a00f0000",
    49  			expectInstruction: &Instruction{
    50  				BaseVariant: bin.BaseVariant{
    51  					TypeID: bin.TypeIDFromUint8(1),
    52  					Impl: &RequestHeapFrame{
    53  						HeapSize: 4000,
    54  					},
    55  				},
    56  			},
    57  		},
    58  		{
    59  			name:    "SetComputeUnitLimit",
    60  			hexData: "02c05c1500",
    61  			expectInstruction: &Instruction{
    62  				BaseVariant: bin.BaseVariant{
    63  					TypeID: bin.TypeIDFromUint8(2),
    64  					Impl: &SetComputeUnitLimit{
    65  						Units: 1400000,
    66  					},
    67  				},
    68  			},
    69  		},
    70  		{
    71  			name:    "SetComputeUnitPrice",
    72  			hexData: "03e803000000000000",
    73  			expectInstruction: &Instruction{
    74  				BaseVariant: bin.BaseVariant{
    75  					TypeID: bin.TypeIDFromUint8(3),
    76  					Impl: &SetComputeUnitPrice{
    77  						MicroLamports: 1000,
    78  					},
    79  				},
    80  			},
    81  		},
    82  	}
    83  
    84  	t.Run("should encode", func(t *testing.T) {
    85  		for _, test := range tests {
    86  			t.Run(test.name, func(t *testing.T) {
    87  				buf := new(bytes.Buffer)
    88  				err := bin.NewBinEncoder(buf).Encode(test.expectInstruction)
    89  				require.NoError(t, err)
    90  
    91  				encodedHex := hex.EncodeToString(buf.Bytes())
    92  				require.Equal(t, test.hexData, encodedHex)
    93  			})
    94  		}
    95  	})
    96  
    97  	t.Run("should decode", func(t *testing.T) {
    98  		for _, test := range tests {
    99  			t.Run(test.name, func(t *testing.T) {
   100  				data, err := hex.DecodeString(test.hexData)
   101  				require.NoError(t, err)
   102  				var instruction *Instruction
   103  				err = bin.NewBinDecoder(data).Decode(&instruction)
   104  				require.NoError(t, err)
   105  				assert.Equal(t, test.expectInstruction, instruction)
   106  			})
   107  		}
   108  	})
   109  
   110  }