Initial commit

This commit is contained in:
Claudio Maggioni 2022-11-29 17:25:52 +01:00
parent 1b66edcd96
commit c15c687013
6 changed files with 479 additions and 100 deletions

View file

@ -1,92 +1,10 @@
# Group 1
# SDM assigment 04
Classes come from [https://github.com/apache/commons-lang](https://github.com/apache/commons-lang)
revision `770e72d2f78361b14f3fe27caea41e5977d3c638`
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
## Run Tests
```
cd existing_repo
git remote add origin https://gitlab.com/usi-si-teaching/msde/2022-2023/software-design-and-modeling/assignment-4-dbc/group-1.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.com/usi-si-teaching/msde/2022-2023/software-design-and-modeling/assignment-4-dbc/group-1/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
mvn clean test
```

23
pom.xml
View file

@ -15,6 +15,11 @@
</properties>
<dependencies>
<dependency>
<groupId>ch.usi.si.codelounge</groupId>
<artifactId>jSicko</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
@ -40,18 +45,12 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
<arg>-Xplugin:JSickoContractCompiler</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

View file

@ -0,0 +1,255 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.usi.inf.sdm.sdm04;
import org.apache.commons.lang3.Validate;
import java.util.Objects;
/**
* Provides IEEE-754r variants of NumberUtils methods.
*
* <p>See: <a href="https://en.wikipedia.org/wiki/IEEE_754r">https://en.wikipedia.org/wiki/IEEE_754r</a></p>
*
* @since 2.4
*/
public class IEEE754rUtils implements IEEE754rUtilsContracts {
/**
* Returns the minimum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws NullPointerException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from min(double[]) to min(double...)
*/
@Requires("non_null_non_empty_arg")
@Ensures("is_minimum_element")
public double min(final double... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns min
double min = array[0];
for (int i = 1; i < array.length; i++) {
min = min(array[i], min);
}
return min;
}
/**
* Returns the minimum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws NullPointerException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from min(float[]) to min(float...)
*/
public float min(final float... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns min
float min = array[0];
for (int i = 1; i < array.length; i++) {
min = min(array[i], min);
}
return min;
}
/**
* Gets the minimum of three {@code double} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the smallest of the values
*/
public double min(final double a, final double b, final double c) {
return min(min(a, b), c);
}
/**
* Gets the minimum of two {@code double} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @return the smallest of the values
*/
public double min(final double a, final double b) {
if (Double.isNaN(a)) {
return b;
}
if (Double.isNaN(b)) {
return a;
}
return Math.min(a, b);
}
/**
* Gets the minimum of three {@code float} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the smallest of the values
*/
public float min(final float a, final float b, final float c) {
return min(min(a, b), c);
}
/**
* Gets the minimum of two {@code float} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @return the smallest of the values
*/
public float min(final float a, final float b) {
if (Float.isNaN(a)) {
return b;
}
if (Float.isNaN(b)) {
return a;
}
return Math.min(a, b);
}
/**
* Returns the maximum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws NullPointerException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from max(double[]) to max(double...)
*/
public double max(final double... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns max
double max = array[0];
for (int j = 1; j < array.length; j++) {
max = max(array[j], max);
}
return max;
}
/**
* Returns the maximum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws NullPointerException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from max(float[]) to max(float...)
*/
public float max(final float... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns max
float max = array[0];
for (int j = 1; j < array.length; j++) {
max = max(array[j], max);
}
return max;
}
/**
* Gets the maximum of three {@code double} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public double max(final double a, final double b, final double c) {
return max(max(a, b), c);
}
/**
* Gets the maximum of two {@code double} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @return the largest of the values
*/
public double max(final double a, final double b) {
if (Double.isNaN(a)) {
return b;
}
if (Double.isNaN(b)) {
return a;
}
return Math.max(a, b);
}
/**
* Gets the maximum of three {@code float} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public float max(final float a, final float b, final float c) {
return max(max(a, b), c);
}
/**
* Gets the maximum of two {@code float} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @return the largest of the values
*/
public float max(final float a, final float b) {
if (Float.isNaN(a)) {
return b;
}
if (Float.isNaN(b)) {
return a;
}
return Math.max(a, b);
}
}

View file

@ -0,0 +1,37 @@
/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/
*
* Copyright 2014-2017 Vavr, http://vavr.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.usi.inf.sdm.sdm04;
import ch.usi.si.codelounge.jsicko.Contract;
import java.util.stream.DoubleStream;
@SuppressWarnings("unused") // methods used by jSicko
public interface IEEE754rUtilsContracts extends Contract {
@Pure
default boolean non_null_non_empty_arg(double[] array) {
return array != null && array.length > 0;
}
@Pure
default boolean is_minimum_element(final double returns, double[] array) {
return DoubleStream.of(array).filter(value -> !Double.isNaN(value)).allMatch(e -> returns <= e);
}
}

View file

@ -0,0 +1,65 @@
package ch.usi.inf.sdm.sdm04;
import ch.usi.si.codelounge.jsicko.Contract;
import java.util.Optional;
import java.util.function.Supplier;
public class Option<T> extends Optional<T> Contract {
private final io.vavr.control.Option<T> backing;
public Option(final io.vavr.control.Option<T> backing) {
this.backing = backing;
}
public static <T> Option<T> none() {
return new Option<>(io.vavr.control.Option.none());
}
public static <T> Option<T> some(final T value) {
return new Option<>(io.vavr.control.Option.some(value));
}
static <T> Option<T> of(T value) {
return value == null ? none() : some(value);
}
static <T> Option<T> narrow(Option<? extends T> option) {
return new Option<>(io.vavr.control.Option.narrow(option.backing));
}
static <T> Option<T> when(boolean condition, Supplier<? extends T> supplier) {
return new Option<>(io.vavr.control.Option.when(condition, supplier));
}
static <T> Option<T> when(boolean condition, T value) {
return new Option<>(io.vavr.control.Option.when(condition, value));
}
static <T> Option<T> ofOptional(Optional<? extends T> optional) {
return new Option<>(io.vavr.control.Option.ofOptional(optional));
}
@Override
@Pure
public boolean isEmpty() {
return backing.isEmpty();
}
@Override
public String stringPrefix() {
return backing.stringPrefix();
}
@Override
@Requires("isNotEmpty")
public T get() {
return backing.get();
}
@Pure
@SuppressWarnings("unused")
public boolean isNotEmpty() {
return !isEmpty();
}
}

View file

@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.usi.inf.sdm.sdm04;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/**
* Unit tests {@link org.apache.commons.lang3.math.IEEE754rUtils}.
*/
public class IEEE754rUtilsTest {
@Test
public void testConstructorExists() {
new IEEE754rUtils();
}
@Test
public void testEnforceExceptions() {
assertThrows(
NullPointerException.class,
() -> new IEEE754rUtils().min( (float[]) null),
"IllegalArgumentException expected for null input");
// Changed the exception type to throwable so a
assertThrows(
Throwable.class,
new IEEE754rUtils()::min,
"IllegalArgumentException expected for empty input");
assertThrows(
Throwable.class,
() -> new IEEE754rUtils().max((float[]) null),
"IllegalArgumentException expected for null input");
assertThrows(
Throwable.class,
new IEEE754rUtils()::max,
"IllegalArgumentException expected for empty input");
assertThrows(
Throwable.class,
() -> new IEEE754rUtils().min( (double[]) null),
"IllegalArgumentException expected for null input");
assertThrows(
Throwable.class,
new IEEE754rUtils()::min,
"IllegalArgumentException expected for empty input");
assertThrows(
Throwable.class,
() -> new IEEE754rUtils().max( (double[]) null),
"IllegalArgumentException expected for null input");
assertThrows(
Throwable.class,
new IEEE754rUtils()::max,
"IllegalArgumentException expected for empty input");
}
@Test
public void testLang381() {
assertEquals(1.2, new IEEE754rUtils().min(1.2, 2.5, Double.NaN), 0.01);
assertEquals(2.5, new IEEE754rUtils().max(1.2, 2.5, Double.NaN), 0.01);
assertTrue(Double.isNaN(new IEEE754rUtils().max(Double.NaN, Double.NaN, Double.NaN)));
assertEquals(1.2f, new IEEE754rUtils().min(1.2f, 2.5f, Float.NaN), 0.01);
assertEquals(2.5f, new IEEE754rUtils().max(1.2f, 2.5f, Float.NaN), 0.01);
assertTrue(Float.isNaN(new IEEE754rUtils().max(Float.NaN, Float.NaN, Float.NaN)));
final double[] a = { 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
assertEquals(42.0, new IEEE754rUtils().max(a), 0.01);
assertEquals(1.2, new IEEE754rUtils().min(a), 0.01);
final double[] b = { Double.NaN, 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
assertEquals(42.0, new IEEE754rUtils().max(b), 0.01);
assertEquals(1.2, new IEEE754rUtils().min(b), 0.01);
final float[] aF = { 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
assertEquals(1.2f, new IEEE754rUtils().min(aF), 0.01);
assertEquals(42.0f, new IEEE754rUtils().max(aF), 0.01);
final float[] bF = { Float.NaN, 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
assertEquals(1.2f, new IEEE754rUtils().min(bF), 0.01);
assertEquals(42.0f, new IEEE754rUtils().max(bF), 0.01);
}
}