From 41e6049a43cdfb7f5d831b2672efaf1be3a4d2ce Mon Sep 17 00:00:00 2001 From: slowy07 Date: Sun, 18 Feb 2024 21:57:49 +0700 Subject: [PATCH] chore: menambahkan volume kubus Signed-off-by: slowy07 --- OpenSeries/__init__.py | 4 ++-- OpenSeries/dasar.py | 21 +++++++++++++++++++++ testing/dasar.py | 20 ++++++++++++++++++++ testing/main_test.py | 4 ++-- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/OpenSeries/__init__.py b/OpenSeries/__init__.py index b5f2810..aa28ca7 100644 --- a/OpenSeries/__init__.py +++ b/OpenSeries/__init__.py @@ -1,6 +1,6 @@ -from OpenSeries.dasar import bulat, akar, tan_hiperbolik +from OpenSeries.dasar import bulat, akar, tan_hiperbolik, volume_kubus if __name__ == "__main__": - list_dasar: list = [bulat, akar, tan_hiperbolik] + list_dasar: list = [bulat, akar, tan_hiperbolik, volume_kubus] for tipe in range(len(list_dasar)): print(type(list_dasar[tipe])) diff --git a/OpenSeries/dasar.py b/OpenSeries/dasar.py index ed09f33..38ed07d 100644 --- a/OpenSeries/dasar.py +++ b/OpenSeries/dasar.py @@ -66,3 +66,24 @@ def tan_hiperbolik(x: np.ndarray) -> Union[np.ndarray, error.ErrorTipeData]: if not isinstance(x, np.ndarray): return error.ErrorTipeData(["numpy.ndarray"]) return (2 / (1 + np.exp(-2 * x))) - 1 + + +def volume_kubus( + panjang_sisi: Union[float, int], +) -> Union[float, int, error.ErrorTipeData, error.Error]: + """ + menghitung volume kubus + + Parameter: + panjang_sisi (int atau float): sisi yang akan dihitung + + return: + float atau int: hasil dari perhitungan + ErrorTipeData: error jika diberikan tipe data yang salah + Error: error jika nilai yang diberikan negatif + """ + if not isinstance(panjang_sisi, (float, int)): + return error.ErrorTipeData(["float", "int"]) + if panjang_sisi < 0: + return error.Error("panjang_sisi hanya menerima nilai positif") + return pow(panjang_sisi, 3) diff --git a/testing/dasar.py b/testing/dasar.py index 2554326..956eaf4 100644 --- a/testing/dasar.py +++ b/testing/dasar.py @@ -58,3 +58,23 @@ def test_tan_invalid_input(self): input = 5 with self.assertRaises(error.ErrorTipeData): raise opseries.tan_hiperbolik(input) + + +class TestVolumeKubus(unittest.TestCase): + def test_volume_nilai_integer(self): + hasil = opseries.volume_kubus(3) + self.assertEqual(hasil, 27) + + def test_volume_nilai_float(self): + hasil = opseries.volume_kubus(3.0) + self.assertEqual(hasil, 27.0) + + def test_nilai_invalid_str(self): + hasil = opseries.volume_kubus("20") + with self.assertRaises(error.ErrorTipeData): + raise hasil + + def test_nilai_invalid_minus(self): + hasil = opseries.volume_kubus(-1) + with self.assertRaises(error.Error): + raise hasil diff --git a/testing/main_test.py b/testing/main_test.py index ef58ab9..ba9c11d 100644 --- a/testing/main_test.py +++ b/testing/main_test.py @@ -39,7 +39,7 @@ from testing.statistika_test import TestFungsiEntropy, TestFungiStandardDeviasi -from testing.dasar import TestBulat, TestAkar, TestTanHiperbolik +from testing.dasar import TestBulat, TestAkar, TestTanHiperbolik, TestVolumeKubus if __name__ == "__main__": testing_matematika: list = [ @@ -84,7 +84,7 @@ TestAngkaSegitiga, ] - testing_dasar: list = [TestBulat, TestAkar, TestTanHiperbolik] + testing_dasar: list = [TestBulat, TestAkar, TestTanHiperbolik, TestVolumeKubus] all_tests = unittest.TestSuite()