Skip to content

Commit

Permalink
#166 Fix throw Exception for LinkedList
Browse files Browse the repository at this point in the history
  • Loading branch information
frangte committed Jul 17, 2017
1 parent 0773c39 commit 6cdbb83
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 5 deletions.
45 changes: 45 additions & 0 deletions java/lang/NoSuchElementException/NoSuchElementException.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2016 Food Tiny Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "NoSuchElementException.hpp"

using namespace Java::Lang;

NoSuchElementException::NoSuchElementException() : RuntimeException() {

}

NoSuchElementException::NoSuchElementException(String message) : RuntimeException(message) {

}

NoSuchElementException::NoSuchElementException(Throwable *cause) : RuntimeException(cause) {

}

NoSuchElementException::NoSuchElementException(String message, Throwable *cause) : RuntimeException(message, cause) {

}
41 changes: 41 additions & 0 deletions java/lang/NoSuchElementException/NoSuchElementException.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2016 Food Tiny Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef NATIVE_NOSUCHELEMENTEXCEPTION_HPP
#define NATIVE_NOSUCHELEMENTEXCEPTION_HPP

#include "../RuntimeException/RuntimeException.hpp"
#include "../../Lang.hpp"

class NoSuchElementException : public RuntimeException {
public:
NoSuchElementException();
NoSuchElementException(String message);
NoSuchElementException(Throwable *cause);
NoSuchElementException(String message, Throwable *cause);
};

#endif //NATIVE_NOSUCHELEMENTEXCEPTION_HPP
61 changes: 61 additions & 0 deletions java/lang/NoSuchElementException/NoSuchElementExceptionTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (c) 2016 Food Tiny Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

extern "C" {
#include "../../../unit_test.h"
};

#include "NoSuchElementException.hpp"

using namespace Java::Lang;

TEST (JavaLang, NoSuchElementExceptionConstructor) {
// Constructs a new NoSuchElementException with null as its detail message.
NoSuchElementException NoSuchElementExceptionWithNullMessage;
ASSERT_STR("", NoSuchElementExceptionWithNullMessage.getMessage().toString());

// Constructs a new NoSuchElementException with the specified detail message.
NoSuchElementException NoSuchElementExceptionWithMessage = NoSuchElementException("NoSuchElementException with the specified message");
ASSERT_STR("NoSuchElementException with the specified message", NoSuchElementExceptionWithMessage.getMessage().toString());

// Constructs a new NoSuchElementException with the specified detail message and cause.
NoSuchElementException NoSuchElementExceptionWithMessageAndCause = NoSuchElementException("NoSuchElementException with the specified message and cause", &NoSuchElementExceptionWithMessage);
ASSERT_STR("NoSuchElementException with the specified message and cause", NoSuchElementExceptionWithMessageAndCause.getMessage().toString());
ASSERT_STR("NoSuchElementException with the specified message", NoSuchElementExceptionWithMessageAndCause.getCause()->getMessage().toString());

// Constructs a new NoSuchElementException with the specified cause.
NoSuchElementException NoSuchElementExceptionWithCause = NoSuchElementException(&NoSuchElementExceptionWithMessageAndCause);
ASSERT_STR("NoSuchElementException with the specified message and cause", NoSuchElementExceptionWithCause.getCause()->getMessage().toString());
ASSERT_STR("NoSuchElementException with the specified message", NoSuchElementExceptionWithCause.getCause()->getCause()->getMessage().toString());
}

TEST (JavaLang, NoSuchElementExceptionTryCatch) {
try {
throw NoSuchElementException("Throw NoSuchElementException");
} catch (Exception e) {
ASSERT_STR("Throw NoSuchElementException", e.getMessage().toString());
}
}
12 changes: 7 additions & 5 deletions java/util/LinkedList/LinkedList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "../List/List.hpp"
#include "../Queue/Queue.hpp"
#include "../../lang/Exception/Exception.hpp"
#include "../../lang/IndexOutOfBoundsException/IndexOutOfBoundsException.hpp"
#include "../../lang/NoSuchElementException/NoSuchElementException.hpp"

using namespace Java::Lang;

Expand Down Expand Up @@ -210,7 +212,7 @@ namespace Java {
*/
E get(int index) {
if(index < 0 || index > this->nodeSize - 1) {
throw Java::Lang::Exception("Index is out of bounds!");
throw IndexOutOfBoundsException("Index is out of bounds!");
}
return node0(index)->element;
}
Expand All @@ -222,7 +224,7 @@ namespace Java {
*/
E getFirst() {
if (this->first == NULL) {
throw Java::Lang::Exception("List is empty!");
throw NoSuchElementException();
}
return this->first->element;
}
Expand All @@ -234,7 +236,7 @@ namespace Java {
*/
E getLast() {
if (this->last == NULL) {
throw Java::Lang::Exception("List is empty!");
throw NoSuchElementException();
}
return this->last->element;
}
Expand Down Expand Up @@ -599,7 +601,7 @@ namespace Java {

E unlinkFirst() {
if (this->first == NULL) {
throw Java::Lang::Exception("List is empty");
throw NoSuchElementException();
}

Node<E> *temp = this->first;
Expand All @@ -613,7 +615,7 @@ namespace Java {

E unlinkLast() {
if (this->last == NULL) {
throw Java::Lang::Exception("List is empty");
throw NoSuchElementException();
}

Node<E> *temp = this->last;
Expand Down

0 comments on commit 6cdbb83

Please sign in to comment.