mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
added *gigawords DataConverter
This commit is contained in:
committed by
Dan Christian Bogos
parent
2867be100a
commit
8e89b84e03
@@ -1165,6 +1165,7 @@ const (
|
||||
MetaTpDispatchers = "*tp_dispatchers"
|
||||
MetaDurationSeconds = "*duration_seconds"
|
||||
MetaDurationNanoseconds = "*duration_nanoseconds"
|
||||
MetaGigawords = "*gigawords"
|
||||
CapAttributes = "Attributes"
|
||||
CapResourceAllocation = "ResourceAllocation"
|
||||
CapMaxUsage = "MaxUsage"
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/url"
|
||||
@@ -129,6 +130,8 @@ func NewDataConverter(params string) (conv DataConverter, err error) {
|
||||
return NewRandomConverter(params[len(MetaRandom)+1:])
|
||||
case strings.HasPrefix(params, MetaStrip):
|
||||
return NewStripConverter(params)
|
||||
case strings.HasPrefix(params, MetaGigawords):
|
||||
return new(GigawordsConverter), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported converter definition: <%s>", params)
|
||||
}
|
||||
@@ -787,3 +790,15 @@ func (URLEncodeConverter) Convert(in any) (any, error) {
|
||||
}
|
||||
return parsedURL.String(), nil
|
||||
}
|
||||
|
||||
// GigawordsConverter converts a value in Gigawords to octects
|
||||
type GigawordsConverter struct{}
|
||||
|
||||
func (GigawordsConverter) Convert(in any) (any, error) {
|
||||
gigawordsValue, err := IfaceAsInt64(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
totalOctects := (gigawordsValue * int64(math.Pow(2, 32))) // 2^32
|
||||
return totalOctects, nil
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"math"
|
||||
"net"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -1814,3 +1815,82 @@ func TestStripConverterConvert(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestGigaWordsConverter(t *testing.T) {
|
||||
converter := GigawordsConverter{}
|
||||
multiplier := int64(4294967296)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expectedValue int64
|
||||
expectError bool
|
||||
errorContains string
|
||||
}{
|
||||
{
|
||||
name: "Input Zero (int)",
|
||||
input: int(0),
|
||||
expectedValue: 0,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Input Zero (int32)",
|
||||
input: int32(0),
|
||||
expectedValue: 0,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Input One (int)",
|
||||
input: int(1),
|
||||
expectedValue: multiplier,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Input Two (int64)",
|
||||
input: int64(2),
|
||||
expectedValue: 2 * multiplier,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Input Three (string)",
|
||||
input: "3",
|
||||
expectedValue: 3 * multiplier,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Input Nil",
|
||||
input: nil,
|
||||
expectedValue: 0,
|
||||
expectError: true,
|
||||
errorContains: "cannot convert",
|
||||
},
|
||||
{
|
||||
name: "Input Invalid String",
|
||||
input: "abc",
|
||||
expectedValue: 0,
|
||||
expectError: true,
|
||||
errorContains: "strconv.ParseInt: parsing \"abc\": invalid syntax",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
output, err := converter.Convert(tc.input)
|
||||
if tc.expectError {
|
||||
if err == nil || !strings.Contains(err.Error(), tc.errorContains) {
|
||||
t.Errorf("Expected error '%s', but got '%v'", tc.errorContains, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, but got: %v", err)
|
||||
}
|
||||
|
||||
outputInt64, ok := output.(int64)
|
||||
if !ok {
|
||||
t.Fatalf("Expected output type int64, but got %T (%v)", output, output)
|
||||
}
|
||||
if outputInt64 != tc.expectedValue {
|
||||
t.Errorf("Expected output %d, but got %d", tc.expectedValue, outputInt64)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user