From a201c6ae5d4bf80a66087f0ec4a5dd81adb847f4 Mon Sep 17 00:00:00 2001 From: Rui Azevedo Date: Tue, 18 Jun 2024 14:41:16 +0100 Subject: [PATCH] Testdata for transpiling Java into Go Part of #201 --- .../balancedBrackets/balancedBrackets.go | 3 + .../balancedBrackets/balancedBrackets_test.go | 51 ++++++++++++++++ .../java/transpile/balancedBrackets/go.mod | 5 ++ .../implementation/BalancedBrackets.java | 21 +++++++ .../transpile/binarySearch/binarySearch.go | 4 ++ .../binarySearch/binarySearch_test.go | 57 ++++++++++++++++++ testdata/java/transpile/binarySearch/go.mod | 5 ++ .../implementation/BinarySearch.java | 24 ++++++++ .../cascadingIfElse/cascadingIfElse.go | 4 ++ .../cascadingIfElse/cascadingIfElse_test.go | 41 +++++++++++++ .../java/transpile/cascadingIfElse/go.mod | 5 ++ .../implementation/CascadingIfElse.java | 13 +++++ .../java/transpile/pascalsTriangle/go.mod | 5 ++ .../implementation/PascalsTriangle.java | 21 +++++++ .../pascalsTriangle/pascalsTriangle.go | 4 ++ .../pascalsTriangle/pascalsTriangle_test.go | 58 +++++++++++++++++++ testdata/java/transpile/sort/go.mod | 5 ++ .../transpile/sort/implementation/Sort.java | 12 ++++ testdata/java/transpile/sort/sort.go | 4 ++ testdata/java/transpile/sort/sort_test.go | 51 ++++++++++++++++ 20 files changed, 393 insertions(+) create mode 100644 testdata/java/transpile/balancedBrackets/balancedBrackets.go create mode 100644 testdata/java/transpile/balancedBrackets/balancedBrackets_test.go create mode 100644 testdata/java/transpile/balancedBrackets/go.mod create mode 100644 testdata/java/transpile/balancedBrackets/implementation/BalancedBrackets.java create mode 100644 testdata/java/transpile/binarySearch/binarySearch.go create mode 100644 testdata/java/transpile/binarySearch/binarySearch_test.go create mode 100644 testdata/java/transpile/binarySearch/go.mod create mode 100644 testdata/java/transpile/binarySearch/implementation/BinarySearch.java create mode 100644 testdata/java/transpile/cascadingIfElse/cascadingIfElse.go create mode 100644 testdata/java/transpile/cascadingIfElse/cascadingIfElse_test.go create mode 100644 testdata/java/transpile/cascadingIfElse/go.mod create mode 100644 testdata/java/transpile/cascadingIfElse/implementation/CascadingIfElse.java create mode 100644 testdata/java/transpile/pascalsTriangle/go.mod create mode 100644 testdata/java/transpile/pascalsTriangle/implementation/PascalsTriangle.java create mode 100644 testdata/java/transpile/pascalsTriangle/pascalsTriangle.go create mode 100644 testdata/java/transpile/pascalsTriangle/pascalsTriangle_test.go create mode 100644 testdata/java/transpile/sort/go.mod create mode 100644 testdata/java/transpile/sort/implementation/Sort.java create mode 100644 testdata/java/transpile/sort/sort.go create mode 100644 testdata/java/transpile/sort/sort_test.go diff --git a/testdata/java/transpile/balancedBrackets/balancedBrackets.go b/testdata/java/transpile/balancedBrackets/balancedBrackets.go new file mode 100644 index 00000000..68c60fa6 --- /dev/null +++ b/testdata/java/transpile/balancedBrackets/balancedBrackets.go @@ -0,0 +1,3 @@ +package balancedBrackets + +func hasBalancedBrackets(charArray string) bool {} diff --git a/testdata/java/transpile/balancedBrackets/balancedBrackets_test.go b/testdata/java/transpile/balancedBrackets/balancedBrackets_test.go new file mode 100644 index 00000000..9980831c --- /dev/null +++ b/testdata/java/transpile/balancedBrackets/balancedBrackets_test.go @@ -0,0 +1,51 @@ +package balancedBrackets + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSymflowerHasBalancedBrackets(t *testing.T) { + type testCase struct { + Name string + + CharArray string + + ExpectedBool bool + } + + validate := func(t *testing.T, tc *testCase) { + t.Run(tc.Name, func(t *testing.T) { + actualBool := hasBalancedBrackets(tc.CharArray) + + assert.Equal(t, tc.ExpectedBool, actualBool) + }) + } + + validate(t, &testCase{ + CharArray: "", + + ExpectedBool: true, + }) + validate(t, &testCase{ + CharArray: "[", + + ExpectedBool: false, + }) + validate(t, &testCase{ + CharArray: "[[[]]", + + ExpectedBool: false, + }) + validate(t, &testCase{ + CharArray: "[[]]", + + ExpectedBool: true, + }) + validate(t, &testCase{ + CharArray: "[[[[]]]]", + + ExpectedBool: true, + }) +} diff --git a/testdata/java/transpile/balancedBrackets/go.mod b/testdata/java/transpile/balancedBrackets/go.mod new file mode 100644 index 00000000..91021b5d --- /dev/null +++ b/testdata/java/transpile/balancedBrackets/go.mod @@ -0,0 +1,5 @@ +module balancedBrackets + +go 1.21.5 + +require github.com/stretchr/testify v1.9.0 // indirect diff --git a/testdata/java/transpile/balancedBrackets/implementation/BalancedBrackets.java b/testdata/java/transpile/balancedBrackets/implementation/BalancedBrackets.java new file mode 100644 index 00000000..28aca1f0 --- /dev/null +++ b/testdata/java/transpile/balancedBrackets/implementation/BalancedBrackets.java @@ -0,0 +1,21 @@ +// Copyright (C) rosettacode.org +package com.eval; + +public class BalancedBrackets { + static boolean hasBalancedBrackets(char[] charArray) { + int brackets = 0; + for (char ch : charArray) { + if (ch == '[') { + brackets++; + } else if (ch == ']') { + brackets--; + } else { + return false; // Non-bracket characters. + } + if (brackets < 0) { // Closing bracket before opening bracket. + return false; + } + } + return brackets == 0; + } +} diff --git a/testdata/java/transpile/binarySearch/binarySearch.go b/testdata/java/transpile/binarySearch/binarySearch.go new file mode 100644 index 00000000..497ea338 --- /dev/null +++ b/testdata/java/transpile/binarySearch/binarySearch.go @@ -0,0 +1,4 @@ +package binarySearch + +func binarySearch(a []int, x int) int { +} diff --git a/testdata/java/transpile/binarySearch/binarySearch_test.go b/testdata/java/transpile/binarySearch/binarySearch_test.go new file mode 100644 index 00000000..c6de6c60 --- /dev/null +++ b/testdata/java/transpile/binarySearch/binarySearch_test.go @@ -0,0 +1,57 @@ +package binarySearch + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSymflowerBinarySearch(t *testing.T) { + type testCase struct { + Name string + + A []int + X int + + ExpectedInt int + } + + validate := func(t *testing.T, tc *testCase) { + t.Run(tc.Name, func(t *testing.T) { + actualInt := binarySearch(tc.A, tc.X) + + assert.Equal(t, tc.ExpectedInt, actualInt) + }) + } + + validate(t, &testCase{ + A: []int(nil), + X: 0, + + ExpectedInt: -1, + }) + validate(t, &testCase{ + A: []int{0}, + X: 5, + + ExpectedInt: -1, + }) + validate(t, &testCase{ + A: []int{1, 2, 3, 4, 5}, + X: 6, + + ExpectedInt: -1, + }) + validate(t, &testCase{ + A: []int{1, 2, 3, 4, 5}, + X: 3, + + ExpectedInt: 2, + }) + validate(t, &testCase{ + A: []int{1, 5, 10, 15, 20, 25}, + X: 25, + + ExpectedInt: 5, + }) +} diff --git a/testdata/java/transpile/binarySearch/go.mod b/testdata/java/transpile/binarySearch/go.mod new file mode 100644 index 00000000..9e7006bc --- /dev/null +++ b/testdata/java/transpile/binarySearch/go.mod @@ -0,0 +1,5 @@ +module binarySearch + +go 1.21.5 + +require github.com/stretchr/testify v1.9.0 // indirect diff --git a/testdata/java/transpile/binarySearch/implementation/BinarySearch.java b/testdata/java/transpile/binarySearch/implementation/BinarySearch.java new file mode 100644 index 00000000..80a0afbf --- /dev/null +++ b/testdata/java/transpile/binarySearch/implementation/BinarySearch.java @@ -0,0 +1,24 @@ +package com.eval; + +class BinarySearch { + static int binarySearch(int[] a, int x) { + int index = -1; + + int min = 0; + int max = a.length - 1; + + while (index == -1 && min <= max) { + int m = (min + max) / 2; + + if (x == a[m]) { + index = m; + } else if (x < a[m]) { + max = m - 1; + } else { + min = m + 1; + } + } + + return index; + } +} diff --git a/testdata/java/transpile/cascadingIfElse/cascadingIfElse.go b/testdata/java/transpile/cascadingIfElse/cascadingIfElse.go new file mode 100644 index 00000000..d0497469 --- /dev/null +++ b/testdata/java/transpile/cascadingIfElse/cascadingIfElse.go @@ -0,0 +1,4 @@ +package cascadingIfElse + +func cascadingIfElse(i int) int { +} diff --git a/testdata/java/transpile/cascadingIfElse/cascadingIfElse_test.go b/testdata/java/transpile/cascadingIfElse/cascadingIfElse_test.go new file mode 100644 index 00000000..ffd493e9 --- /dev/null +++ b/testdata/java/transpile/cascadingIfElse/cascadingIfElse_test.go @@ -0,0 +1,41 @@ +package cascadingIfElse + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSymflowerCascadingIfElse(t *testing.T) { + type testCase struct { + Name string + + I int + + ExpectedInt int + } + + validate := func(t *testing.T, tc *testCase) { + t.Run(tc.Name, func(t *testing.T) { + actualInt := cascadingIfElse(tc.I) + + assert.Equal(t, tc.ExpectedInt, actualInt) + }) + } + + validate(t, &testCase{ + I: 0, + + ExpectedInt: 5, + }) + validate(t, &testCase{ + I: 1, + + ExpectedInt: 2, + }) + validate(t, &testCase{ + I: 3, + + ExpectedInt: 4, + }) +} diff --git a/testdata/java/transpile/cascadingIfElse/go.mod b/testdata/java/transpile/cascadingIfElse/go.mod new file mode 100644 index 00000000..72b2080a --- /dev/null +++ b/testdata/java/transpile/cascadingIfElse/go.mod @@ -0,0 +1,5 @@ +module cascadingIfElse + +go 1.21.5 + +require github.com/stretchr/testify v1.9.0 // indirect diff --git a/testdata/java/transpile/cascadingIfElse/implementation/CascadingIfElse.java b/testdata/java/transpile/cascadingIfElse/implementation/CascadingIfElse.java new file mode 100644 index 00000000..a510ee4c --- /dev/null +++ b/testdata/java/transpile/cascadingIfElse/implementation/CascadingIfElse.java @@ -0,0 +1,13 @@ +package com.eval; + +class CascadingIfElse { + static int cascadingIfElse(int i) { + if (i == 1) { + return 2; + } else if (i == 3) { + return 4; + } else { + return 5; + } + } +} diff --git a/testdata/java/transpile/pascalsTriangle/go.mod b/testdata/java/transpile/pascalsTriangle/go.mod new file mode 100644 index 00000000..3d0b31bc --- /dev/null +++ b/testdata/java/transpile/pascalsTriangle/go.mod @@ -0,0 +1,5 @@ +module pascalsTriangle + +go 1.21.5 + +require github.com/stretchr/testify v1.9.0 // indirect diff --git a/testdata/java/transpile/pascalsTriangle/implementation/PascalsTriangle.java b/testdata/java/transpile/pascalsTriangle/implementation/PascalsTriangle.java new file mode 100644 index 00000000..1c74231e --- /dev/null +++ b/testdata/java/transpile/pascalsTriangle/implementation/PascalsTriangle.java @@ -0,0 +1,21 @@ +package com.eval; + +public class PascalsTriangle { + int[][] generateTriangle(int rows) { + if (rows < 0) { + throw new IllegalArgumentException("Rows can't be negative!"); + } + + int[][] triangle = new int[rows][]; + + for (int i = 0; i < rows; i++) { + triangle[i] = new int[i + 1]; + triangle[i][0] = 1; + for (int j = 1; j < i; j++) { + triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]; + } + triangle[i][i] = 1; + } + return triangle; + } +} diff --git a/testdata/java/transpile/pascalsTriangle/pascalsTriangle.go b/testdata/java/transpile/pascalsTriangle/pascalsTriangle.go new file mode 100644 index 00000000..36519713 --- /dev/null +++ b/testdata/java/transpile/pascalsTriangle/pascalsTriangle.go @@ -0,0 +1,4 @@ +package pascalsTriangle + +func pascalsTriangle(rows int) ([][]int, error) { +} diff --git a/testdata/java/transpile/pascalsTriangle/pascalsTriangle_test.go b/testdata/java/transpile/pascalsTriangle/pascalsTriangle_test.go new file mode 100644 index 00000000..414012fd --- /dev/null +++ b/testdata/java/transpile/pascalsTriangle/pascalsTriangle_test.go @@ -0,0 +1,58 @@ +package pascalsTriangle + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSymflowerPascalsTriangle(t *testing.T) { + type testCase struct { + Name string + + Rows int + + ExpectedSlice [][]int + ExpectedError error + } + + validate := func(t *testing.T, tc *testCase) { + t.Run(tc.Name, func(t *testing.T) { + actualSlice, actualError := pascalsTriangle(tc.Rows) + + assert.Equal(t, tc.ExpectedSlice, actualSlice) + assert.Equal(t, tc.ExpectedError, actualError) + }) + } + + validate(t, &testCase{ + Rows: -1, + + ExpectedSlice: [][]int(nil), + ExpectedError: errors.New("Rows can't be negative!"), + }) + validate(t, &testCase{ + Rows: 0, + + ExpectedSlice: [][]int{}, + }) + validate(t, &testCase{ + Rows: 1, + + ExpectedSlice: [][]int{ + []int{1}, + }, + }) + validate(t, &testCase{ + Rows: 5, + + ExpectedSlice: [][]int{ + []int{1}, + []int{1, 1}, + []int{1, 2, 1}, + []int{1, 3, 3, 1}, + []int{1, 4, 6, 4, 1}, + }, + }) +} diff --git a/testdata/java/transpile/sort/go.mod b/testdata/java/transpile/sort/go.mod new file mode 100644 index 00000000..2be35a28 --- /dev/null +++ b/testdata/java/transpile/sort/go.mod @@ -0,0 +1,5 @@ +module isSorted + +go 1.21.5 + +require github.com/stretchr/testify v1.9.0 // indirect diff --git a/testdata/java/transpile/sort/implementation/Sort.java b/testdata/java/transpile/sort/implementation/Sort.java new file mode 100644 index 00000000..e669875d --- /dev/null +++ b/testdata/java/transpile/sort/implementation/Sort.java @@ -0,0 +1,12 @@ +package com.eval; + +class Sort { + static boolean isSorted(int[] a) { + int i = 0; + while (i < a.length - 1 && a[i] <= a[i + 1]) { + i++; + } + + return i == a.length - 1; + } +} diff --git a/testdata/java/transpile/sort/sort.go b/testdata/java/transpile/sort/sort.go new file mode 100644 index 00000000..8dd6d2f6 --- /dev/null +++ b/testdata/java/transpile/sort/sort.go @@ -0,0 +1,4 @@ +package isSorted + +func isSorted(a []int) bool { +} diff --git a/testdata/java/transpile/sort/sort_test.go b/testdata/java/transpile/sort/sort_test.go new file mode 100644 index 00000000..7840062c --- /dev/null +++ b/testdata/java/transpile/sort/sort_test.go @@ -0,0 +1,51 @@ +package isSorted + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSymflowerIsSorted(t *testing.T) { + type testCase struct { + Name string + + A []int + + ExpectedBool bool + } + + validate := func(t *testing.T, tc *testCase) { + t.Run(tc.Name, func(t *testing.T) { + actualBool := isSorted(tc.A) + + assert.Equal(t, tc.ExpectedBool, actualBool) + }) + } + + validate(t, &testCase{ + A: []int(nil), + + ExpectedBool: false, + }) + validate(t, &testCase{ + A: []int{5, 4, 3, 2, 1}, + + ExpectedBool: false, + }) + validate(t, &testCase{ + A: []int{0}, + + ExpectedBool: true, + }) + validate(t, &testCase{ + A: []int{1, 2, 3, 4, 5}, + + ExpectedBool: true, + }) + validate(t, &testCase{ + A: []int{1, 2, 10, 11, 20, 21}, + + ExpectedBool: true, + }) +}