mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender-pwa.git
synced 2025-08-08 04:39:16 +02:00
3 add occupancy bson endpoint in backend
This commit is contained in:
27
backend/service/functions/filter.go
Normal file
27
backend/service/functions/filter.go
Normal file
@@ -0,0 +1,27 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package functions
|
||||
|
||||
// function to filter an array
|
||||
func Filter[T any](ss []T, test func(T) bool) (ret []T) {
|
||||
for _, s := range ss {
|
||||
if test(s) {
|
||||
ret = append(ret, s)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
103
backend/service/functions/filter_test.go
Normal file
103
backend/service/functions/filter_test.go
Normal file
@@ -0,0 +1,103 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package functions
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Filter_number(t *testing.T) {
|
||||
type args struct {
|
||||
ss []int
|
||||
test func(int) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantRet []int
|
||||
}{
|
||||
{
|
||||
"filter even numbers",
|
||||
args{
|
||||
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
||||
func(i int) bool {
|
||||
return i%2 == 0
|
||||
},
|
||||
},
|
||||
[]int{2, 4, 6, 8, 10},
|
||||
},
|
||||
{
|
||||
"filter smaller than 5",
|
||||
args{
|
||||
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
||||
func(i int) bool {
|
||||
return i < 5
|
||||
},
|
||||
},
|
||||
[]int{1, 2, 3, 4},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotRet := Filter(tt.args.ss, tt.args.test); !reflect.DeepEqual(gotRet, tt.wantRet) {
|
||||
t.Errorf("filter() = %v, want %v", gotRet, tt.wantRet)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Filter_string(t *testing.T) {
|
||||
type args struct {
|
||||
ss []string
|
||||
test func(string) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantRet []string
|
||||
}{
|
||||
{
|
||||
"filter contains a",
|
||||
args{
|
||||
[]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"},
|
||||
func(i string) bool {
|
||||
return strings.Contains(i, "a")
|
||||
},
|
||||
},
|
||||
[]string{"a"},
|
||||
},
|
||||
{
|
||||
"filter starts with prefix 'a'",
|
||||
args{
|
||||
[]string{"alpha", "beta", "a", "ab", "ac", "delta"},
|
||||
func(i string) bool {
|
||||
return strings.HasPrefix(i, "a")
|
||||
},
|
||||
},
|
||||
[]string{"alpha", "a", "ab", "ac"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotRet := Filter(tt.args.ss, tt.args.test); !reflect.DeepEqual(gotRet, tt.wantRet) {
|
||||
t.Errorf("filter() = %v, want %v", gotRet, tt.wantRet)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user