Skip to content
Snippets Groups Projects
Commit 45f53316 authored by Denis Arrivault's avatar Denis Arrivault
Browse files

First commit with operationnal CMake files and gradle script

parents
No related branches found
No related tags found
No related merge requests found
.classpath
.DS_Store
.project
*~
bin/
build/
.gradle/
.settings/
repos/
gradle/
*.class
*.bkp
*.out
.classpath
.project
.externalToolBuilders/
cmake_minimum_required (VERSION 2.8)
project (IMMCOMPLX_JAVA Java)
set(IMMCOMPLX_VERSION "1.0.0")
# Setting source jar install path as a cmake cache variable
# Using default cmake install path if not defined (/usr/lib)
set(JAR_INSTALL_DIR ${CMAKE_INSTALL_PREFIX} CACHE PATH "Path to source jar installation directory")
# Setting lib directory
set(IMMCOMPLX_JAVA_DEPENDENCIES_DIR ${IMMCOMPLX_JAVA_SOURCE_DIR}/lib)
include(FindJava)
include(UseJava)
set(JUNIT_VERSION "4.11")
set(HAMCREST_VERSION "1.3")
enable_testing()
add_subdirectory(src/main)
add_subdirectory(src/test)
Immutable Complex
=================
The Immutable complex project is a java code example that illustrates a presentation on java project building.
The Complex class is mainly adapted from J. Bloch Book : "Effective Java 2nd edition" Addison-Wesley Professional
Description
===========
The project contains CMake and Gradle scripts that enable to build it with both build systems.
Contact
=======
Denis Arrivault : denis.arrivault/NOSPAM<AT>NOSPAM/lif.univ-mrs.fr
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'application'
version = '1.0.0'
applicationName = "ImmutableComplex"
mainClassName = "main.java.Main"
repositories {
jcenter()
//flatDir {dirs 'lib'}
}
dependencies {
//compile "hamcrest:hamcrest-all:1.3"
testCompile "junit:junit:4.11"
// testCompile name: 'junit-4.11'
}
// Local path for Maven depository
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
// Local installation task.
//'installDir' is property that should be defined with : "gradle installLocally -p /install/path"
task installLocally(type: Copy){
from installDist
into project.hasProperty('installDir') ? file(project.getProperty('installDir')) : file('install')
}
installLocally.onlyIf {
project.hasProperty('installDir')
}
File added
File added
/*
* This settings file was auto generated by the Gradle buildInit task
* by 'arrivault' at '03/06/16 14:45' with Gradle 2.3
*
* The settings file is used to specify which projects to include in your build.
* In a single project build this file can be empty or even removed.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at http://gradle.org/docs/2.3/userguide/multi_project_builds.html
*/
/*
// To declare projects as part of a multi-project build use the 'include' method
include 'shared'
include 'api'
include 'services:webservice'
*/
rootProject.name = 'ImmutableComplex'
cmake_minimum_required (VERSION 2.8)
project (IMMCOMPLX_JAVA_MAIN Java)
set(CMAKE_JAVA_TARGET_OUTPUT_NAME immutablecomplex-${IMMCOMPLX_VERSION})
# Compiling the source files into a jar with a main
add_jar(immutablecomplex java/Complex.java java/Main.java
ENTRY_POINT main.java.Main
VERSION ${IMMCOMPLX_VERSION})
get_target_property(JAR immutablecomplex JAR_FILE)
set(IMMCOMPLX_JAR ${JAR} PARENT_SCOPE)
# Installing the jar
message(STATUS "JAR_INSTALL_DIR = ${JAR_INSTALL_DIR}")
install_jar(immutablecomplex ${JAR_INSTALL_DIR})
# Creating the javadoc
create_javadoc(immutablecomplex_doc
FILES java/Complex.java
CLASSPATH ${CMAKE_JAVA_INCLUDE_PATH}
WINDOWTITLE "My Complex Numbers"
DOCTITLE "<h1>My Complex Numbers</h1>"
INSTALLPATH ${JAR_INSTALL_DIR}
AUTHOR TRUE
USE TRUE
VERSION TRUE
)
/**
* Complex class file
*/
package main.java;
/**
* Immutable complex class
* This class is adapted from J. Bloch Book : "Effective Java 2nd edition" Addison-Wesley Professional
* @author J. Bloch
* @author D. Arrivault
*/
public final class Complex {
private final double re;
private final double im;
public Complex(double re, double im){
this.re = re;
this.im = im;
}
//Accessors without mutator
public double realPart(){return re;}
public double imaginaryPart(){return im;}
//Arithmetic operators
public Complex add(Complex c){
return new Complex(re + c.re, im + c.im);
}
public Complex subtract(Complex c){
return new Complex(re - c.re, im - c.im);
}
public Complex multiply(Complex c){
return new Complex(re * c.re - im * c.im, re * c.im + im * c.re);
}
public Complex divide(Complex c){
double tmp = c.re * c.re + c.im * c.im;
return new Complex((re * c.re + im * c.im) / tmp,
(im * c.re - re * c.im) / tmp);
}
//Comparison operator
@Override
public boolean equals(Object o){
if (o == this)
return true;
if (!(o instanceof Complex))
return false;
Complex c = (Complex) o;
return Double.compare(re, c.re)== 0 &&
Double.compare(im, c.im) == 0;
}
@Override
public int hashCode(){
int result = 17 + hashDouble(re);
result = 31 * result + hashDouble(im);
return result;
}
private static int hashDouble(double val){
long longBits = Double.doubleToLongBits(val);
return (int) (longBits ^ (longBits >>> 32));
}
//Display
@Override
public String toString() {
return "(" + re + " + " + im + "i)";
}
//Conjugate
public Complex conj(){
return new Complex(re,-im);
}
}
package main.java;
public class Main {
public static void main(String[] args) {
Complex c1 = new Complex(2,4);
Complex c2 = new Complex(1,2);
System.out.println(c1.divide(c2));
System.out.println(c1.equals(c2));
System.out.println(c1.equals(c2.add(c2)));
}
}
cmake_minimum_required (VERSION 2.6)
project (IMMCOMPLX_JAVA_TEST Java)
set(JUNIT_JAR ${IMMCOMPLX_JAVA_DEPENDENCIES_DIR}/junit-${JUNIT_VERSION}.jar)
set(HAMCREST_JAR ${IMMCOMPLX_JAVA_DEPENDENCIES_DIR}/hamcrest-all-${HAMCREST_VERSION}.jar)
# Compiling of the test file
add_jar(immutablecomplex-test java/ComplexTest.java INCLUDE_JARS ${JUNIT_JAR} ${IMMCOMPLX_JAR})
# Getting the jar's name
get_target_property(IMMCOMPLX_TEST_JAR immutablecomplex-test JAR_FILE)
# Adding test case for ctest
set(CLASSPATH "${JUNIT_JAR}:${HAMCREST_JAR}:${IMMCOMPLX_JAR}:${IMMCOMPLX_TEST_JAR}")
add_test(
NAME test_immutablecomplex
COMMAND ${Java_JAVA_EXECUTABLE} -cp ${CLASSPATH} org.junit.runner.JUnitCore test.java.ComplexTest
)
/**
* Test file for Complex class
*/
package test.java;
import org.junit.Assert;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import main.java.Complex;
/**
* Junit test case for my immutable complex class.
* @author arrivault
*
*/
public class ComplexTest {
private double rmin;
private double rmax;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
rmin = -1e5;
rmax = 1e5;
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link main.java.Complex#Complex(double, double)}.
*/
@Test
public final void testComplex() {
double r = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Assert.assertTrue(new Complex(r, i) instanceof Complex);
}
/**
* Test method for {@link main.java.Complex#realPart()}.
*/
@Test
public final void testRealPart() {
double r = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c = new Complex(r, i);
Assert.assertEquals(Double.compare(r, c.realPart()), 0);
}
/**
* Test method for {@link main.java.Complex#imaginaryPart()}.
*/
@Test
public final void testImaginaryPart() {
double r = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c = new Complex(r, i);
Assert.assertEquals(Double.compare(i, c.imaginaryPart()), 0);
}
/**
* Test method for {@link main.java.Complex#add(main.java.Complex)}.
*/
@Test
public final void testAdd() {
double r1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c1 = new Complex(r1, i1);
double r2 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i2 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c2 = new Complex(r2, i2);
Complex c = c1.add(c2);
Assert.assertEquals(Double.compare(c.realPart(), r1+r2), 0);
Assert.assertEquals(Double.compare(c.imaginaryPart(), i1+i2), 0);
}
/**
* Test method for {@link main.java.Complex#subtract(main.java.Complex)}.
*/
@Test
public final void testSubtract() {
double r1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c1 = new Complex(r1, i1);
double r2 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i2 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c2 = new Complex(r2, i2);
Complex c = c1.subtract(c2);
Assert.assertEquals(Double.compare(c.realPart(), r1-r2), 0);
Assert.assertEquals(Double.compare(c.imaginaryPart(), i1-i2), 0);
}
/**
* Test method for {@link main.java.Complex#multiply(main.java.Complex)}.
*/
@Test
public final void testMulptiply() {
double r1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c1 = new Complex(r1, i1);
double r2 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i2 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c2 = new Complex(r2, i2);
Complex c = c1.multiply(c2);
Assert.assertEquals(Double.compare(c.realPart(), r1*r2 - i1*i2), 0);
Assert.assertEquals(Double.compare(c.imaginaryPart(), i1*r2 + i2*r1), 0);
}
/**
* Test method for {@link main.java.Complex#divide(main.java.Complex)}.
*/
@Test
public final void testDivide() {
double r1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c1 = new Complex(r1, i1);
double r2 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i2 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
if (Double.compare(r2, 0) == 0 && Double.compare(i2, 0) == 0){
r2 = 1.0;
}
Complex c2 = new Complex(r2, i2);
double den = r2*r2 + i2*i2;
Complex num = c1.multiply(c2.conj());
Complex c = c1.divide(c2);
Assert.assertEquals(c.realPart(), num.realPart()/den, 1e-10);
Assert.assertEquals(c.imaginaryPart(), num.imaginaryPart()/den, 1e-10);
}
/**
* Test method for {@link main.java.Complex#equals(java.lang.Object)}.
*/
@Test
public final void testEqualsObject() {
double r1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c1 = new Complex(r1, i1);
Assert.assertTrue(c1.equals(c1));
Assert.assertFalse(c1.equals(r1));
Assert.assertTrue(c1.equals(new Complex(r1,i1)));
Assert.assertFalse(c1.equals(new Complex(r1+1.0,i1+1.0)));
}
/**
* Test method for {@link main.java.Complex#toString()}.
*/
@Test
public final void testToString() {
double r1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
double i1 = ThreadLocalRandom.current().nextDouble(rmin, rmax);
Complex c1 = new Complex(r1, i1);
Assert.assertEquals(c1.toString(),"(" + r1 + " + " + i1 + "i)");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment