From 17dc9ee4966f56746e77529b674784577bd82fad Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Mon, 18 Nov 2024 13:27:46 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20IntNot0(?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- micro/microfunctions.go | 14 ++++++++++++++ micro/microfunctions_test.go | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/micro/microfunctions.go b/micro/microfunctions.go index 82517254..bfa85dff 100644 --- a/micro/microfunctions.go +++ b/micro/microfunctions.go @@ -1230,3 +1230,17 @@ func Substring(input string, StartIndex int, length int) string { return string(asRunes[StartIndex : StartIndex+length]) } + +// IntNot0 - возвращает первое ненулевое значение +func IntNot0(MassInt ...int) int { + Otvet := 0 + + for _, v := range MassInt { + if v != 0 { + Otvet = v + break + } + } + + return Otvet +} diff --git a/micro/microfunctions_test.go b/micro/microfunctions_test.go index 1717e943..6f00ae6c 100644 --- a/micro/microfunctions_test.go +++ b/micro/microfunctions_test.go @@ -1228,3 +1228,23 @@ func TestSubstring(t *testing.T) { } } } + +func TestIntNot0(t *testing.T) { + // Testing when all input integers are 0 + result1 := IntNot0(0, 0, 0) + if result1 != 0 { + t.Errorf("Expected 0, but got %d", result1) + } + + // Testing when some input integers are 0 and some are non-zero + result2 := IntNot0(0, 5, 0, 10) + if result2 != 5 { + t.Errorf("Expected 5, but got %d", result2) + } + + // Testing when all input integers are non-zero + result3 := IntNot0(3, 7, 2) + if result3 != 3 { + t.Errorf("Expected 3, but got %d", result3) + } +}