Skip to content

Commit

Permalink
Preliminary stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ioggstream committed Jun 26, 2024
1 parent e594a9d commit f4f1aab
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 20 deletions.
17 changes: 17 additions & 0 deletions c-project/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CC=gcc
CFLAGS=-fPIC -g

all: example example.so

example: example.o
$(CC) -o example example.o $(CFLAGS)

example.o: example.c
$(CC) -c example.c $(CFLAGS)

example.so: example.o
$(CC) -shared -o example.so example.o

clean:
rm -f example example.o example.so
rm -f example example.o
78 changes: 78 additions & 0 deletions c-project/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* A C file with a library function that appends a buffer to a linked list.
*/

#include <stdlib.h>
#include <string.h>

#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "example.h"


static element *
new_element(void)
{
element *e;

e = malloc(sizeof(*e));
if (e == NULL)
{
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}

return e;
}



element* append(queue *q, char *name) {
element *elem, *prev;

if (!q) return NULL; // Check if the queue pointer itself is NULL

elem = new_element();
if (!elem) return NULL; // Check if memory allocation failed

elem->name = name; // Assuming name is properly allocated and managed outside this function

if (!*q) {
// If the queue is empty, insert the new element at the beginning
*q = elem;
} else {
// Find the last element in the queue
for (prev = *q; prev->forward != NULL; prev = prev->forward)
;
// Insert the new element at the end of the queue
}
insque(elem, prev);

return elem;
}


int main(int argc, char *argv[])
{
element *first, *elem, *prev;
queue *q = malloc(sizeof(queue));

elem = append(q, "1");


printf("Traversing completed list:\n");
elem = first;
do
{
printf(" %s\n", elem->name);
elem = elem->forward;
} while (elem != NULL && elem != first);

if (elem == first)
printf("That was a circular list\n");

exit(EXIT_SUCCESS);
}
8 changes: 8 additions & 0 deletions c-project/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
typedef struct element
{
struct element *forward;
struct element *backward;
char *name;
} element;

typedef element* queue;
2 changes: 1 addition & 1 deletion java-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
293 changes: 293 additions & 0 deletions tests/cJSON.h

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions tests/jpype_script_base.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import jpype


def test_noop():

jpype.startJVM(jpype.getDefaultJVMPath())

Random = jpype.JClass('java.util.Random')
String = jpype.JClass('java.lang.String')
System = jpype.JClass('java.lang.System')
Random = jpype.JClass("java.util.Random")
String = jpype.JClass("java.lang.String")
System = jpype.JClass("java.lang.System")
# Create an instance of java.util.Random
random = Random()
for _ in range(20):
System.out.println(String.format("Random number: %d", random.nextInt()))

# Shutdown the JVM
jpype.shutdownJVM()
jpype.shutdownJVM()
17 changes: 17 additions & 0 deletions tests/test_ctypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import ctypes
import struct
from ctypes import Structure, c_char, c_int, c_longlong, c_uint16
from pathlib import Path

import model
import pytest
import yaml

# Load libraries.
libpmc_so = Path(__file__).parent / "libpmc.so"
messages_yaml = Path(__file__).parent / "messages.yaml"
libpmc = ctypes.CDLL(libpmc_so.as_posix())
libc = ctypes.CDLL("libc.so.6")

# Load testcases.
messages = yaml.safe_load(messages_yaml.read_text())["messages"]
48 changes: 33 additions & 15 deletions tests/test_java.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os
import pytest
from pathlib import Path

import jpype
from jpype import startJVM, shutdownJVM
import pytest
from jpype import shutdownJVM, startJVM

TESTDIR = Path(__file__).parent


@pytest.fixture(scope="session", autouse=False)
Expand All @@ -12,8 +16,8 @@ def setupJVM():
os.environ["TZ"] = "Europe/Rome"
startJVM(
convertStrings=False,
classpath=["java-project/target/guava-28.2-android.jar"],
)
classpath=[(TESTDIR / "guava-28.2-android.jar").as_posix()],
)
yield
shutdownJVM()

Expand All @@ -23,19 +27,24 @@ def test_guava_ascii(setupJVM):
assert Ascii.toLowerCase("EuroPython") == "europython"


@pytest.mark.parametrize("inet_addr,is_valid", [
("1.2.3.4", True),
("3ffe::1", True),
("3ffe::1z", False),
("333.1.1.1", False),
])
@pytest.mark.parametrize(
"inet_addr,is_valid",
[
("1.2.3.4", True),
("3ffe::1", True),
("3ffe::1z", False),
("333.1.1.1", False),
],
)
def test_InetAddress(setupJVM, inet_addr, is_valid):
InetAddress = jpype.JClass("com.google.common.net.InetAddresses")
assert InetAddress.isInetAddress(inet_addr) == is_valid


def test_automatic_imports(setupJVM):
java = jpype.JPackage("java")
import jpype.imports # noqa

jpype.JPackage("java")
from java.lang import String

assert String.format("Hello, %s", "World") == "Hello, World"
Expand All @@ -44,7 +53,16 @@ def test_automatic_imports(setupJVM):
def test_bigdecimal(setupJVM):
BigDecimal = jpype.JClass("java.math.BigDecimal")
a, b = 0.1, 0.2
b_a = BigDecimal(a)
b_b = BigDecimal(b)
assert b_a.add(b_b) == BigDecimal(a+b)
raise ValueError("This test is expected to fail")
c = a + b
j_a = BigDecimal(a)
j_b = BigDecimal(b)
j_c = j_a.add(j_b)

# BigDecimal addition is different from float addition.
assert j_c != BigDecimal(c)

# We can compare the string representation of the two numbers.
assert j_c.toString()[:10] == BigDecimal(c).toString()[:10]

# They eventually differ after the some decimals.
assert j_c.toString() != BigDecimal(c).toString()

0 comments on commit f4f1aab

Please sign in to comment.