Skip to content

Commit

Permalink
JACOBIN-583 Corrected misspelling in name of file, removed block of c…
Browse files Browse the repository at this point in the history
…ommented out tests
  • Loading branch information
platypusguy committed Feb 3, 2025
1 parent fb6000d commit 1626cb4
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 421 deletions.
96 changes: 96 additions & 0 deletions src/gfunction/javaLangStringBuilder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Jacobin VM - A Java virtual machine
* Copyright (c) 2024-5 by the Jacobin Authors. All rights reserved.
* Licensed under Mozilla Public License 2.0 (MPL 2.0) Consult jacobin.org.
*/

package gfunction

import (
"jacobin/excNames"
"jacobin/object"
"jacobin/types"
"testing"
)

func TestStringBuilderInit(t *testing.T) {
// Test case: without capacity
params := []any{object.MakeEmptyObject()}
result := stringBuilderInit(params)
if result != nil {
t.Errorf("Expected nil, got %v", result)
}

// Test case: with capacity
obj := object.MakeEmptyObject()
params = []any{obj, int64(32)}
result = stringBuilderInit(params)
if result != nil {
t.Errorf("Expected nil, got %v", result)
}

if obj.FieldTable["capacity"].Fvalue != int64(32) {
t.Errorf("Expected 32, got %v", result.(*object.Object).FieldTable["capacity"].Fvalue)
}
}

func TestStringBuilderInitString(t *testing.T) {
// Test case: valid string object
obj := object.MakeEmptyObject()
strObj := object.StringObjectFromGoString("Hello")
params := []any{obj, strObj}
result := stringBuilderInitString(params)
if result != nil {
t.Errorf("Expected nil, got %v", result)
}

if object.GoStringFromJavaByteArray(obj.FieldTable["value"].Fvalue.([]types.JavaByte)) !=
object.GoStringFromJavaByteArray(strObj.FieldTable["value"].Fvalue.([]types.JavaByte)) {
t.Errorf("Expected %v, got %v", strObj.FieldTable["value"].Fvalue,
result.(*object.Object).FieldTable["value"].Fvalue)
}

// Test case: invalid parameter type
params = []any{obj, 123}
result = stringBuilderInitString(params)
if result == nil || result.(*GErrBlk).ExceptionType != excNames.IllegalArgumentException {
t.Errorf("Expected IllegalArgumentException, got %s",
excNames.JVMexceptionNames[result.(*GErrBlk).ExceptionType])
}
}

func TestStringBuilderToString(t *testing.T) {
// Test case: convert to string
obj := object.NewStringObject()
obj.FieldTable["value"] = object.Field{Ftype: types.ByteArray, Fvalue: []types.JavaByte{'H', 'e', 'l', 'l', 'o'}}
params := []any{obj}
result := stringBuilderToString(params)
if result == nil {
t.Errorf("Expected non-nil result, got nil")
}
if result == "Hello" {
t.Errorf("Expected 'Hello', got %s", result)
}
}

func TestStringBuilderCapacity(t *testing.T) {
// Test case: get capacity
obj := object.MakeEmptyObject()
obj.FieldTable["capacity"] = object.Field{Ftype: types.Int, Fvalue: int64(16)}
params := []any{obj}
result := stringBuilderCapacity(params)
if result != int64(16) {
t.Errorf("Expected 16, got %v", result)
}
}

func TestStringBuilderLength(t *testing.T) {
// Test case: get length
obj := object.MakeEmptyObject()
obj.FieldTable["count"] = object.Field{Ftype: types.Int, Fvalue: int64(5)}
params := []any{obj}
result := stringBuilderLength(params)
if result != int64(5) {
t.Errorf("Expected 5, got %v", result)
}
}
Loading

0 comments on commit 1626cb4

Please sign in to comment.