Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
# Binaries
bin/
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
# Test binary
*.test

# Code coverage profiles and other test artifacts
# Coverage output
*.out
coverage.*
*.coverprofile
profile.cov
coverage.html

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
# Go workspace
go.work
go.work.sum

# env file
# Environment variables
.env

# Editor/IDE
# .idea/
# .vscode/
# IDE
.idea/
.vscode/
*.swp
*.swo
12 changes: 12 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "2"

linters:
enable:
- errcheck
- staticcheck
- unused
- revive
- govet

run:
timeout: 5m
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: fmt lint test build all

fmt:
go fmt ./...

lint:
golangci-lint run ./...

test:
go test -race ./...

build:
go build -o bin/ ./cmd/app/

all: fmt lint test build
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Lab 1

## Requirements

- [Go](https://go.dev/dl/) 1.26+
- [golangci-lint](https://golangci-lint.run/) 2.x

## Run the application

```bash
make build
./bin/app
```

Or without building:

```bash
go run ./cmd/app/
```

## Run tests

```bash
make test
```

## Run linter

```bash
make lint
```

## Run everything

```bash
make all
```

Runs `fmt`, `lint`, `test`, and `build` sequentially.
Binary file added bin/app
Binary file not shown.
18 changes: 18 additions & 0 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Package comment
package main

import (
"fmt"

methods "github.com/pavlosamtsov/lab1-tooling/internal"
)

func main() {
methods.SayHelloTo("Pavlo")

sum := methods.Sum([]int{15, 20, 25})
fmt.Println(sum)

sorted := methods.Sort([]int{5, 2, 8, 1, 9})
fmt.Println(sorted)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/pavlosamtsov/lab1-tooling

go 1.26.1
31 changes: 31 additions & 0 deletions internal/methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Package methods package comment
package methods

import "fmt"

// SayHelloTo method comment
func SayHelloTo(name string) {
fmt.Println("Hello, " + name + "!")
}

// Sum method comment
func Sum(numbers []int) int {
sum := 0
for _, value := range numbers {
sum = sum + value
}

return sum
}

// Sort method comment
func Sort(numbers []int) []int {
for i := 0; i < len(numbers)-1; i++ {
for j := 0; j < len(numbers)-1-i; j++ {
if numbers[j] > numbers[j+1] {
numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
}
}
}
return numbers
}
104 changes: 104 additions & 0 deletions internal/methods_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package methods

import (
"fmt"
"io"
"os"
"reflect"
"testing"
)

func TestSayHelloTo(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"regular name", "Pavlo", "Hello, Pavlo!\n"},
{"empty name", "", "Hello, !\n"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, w, _ := os.Pipe()
old := os.Stdout
os.Stdout = w

SayHelloTo(tt.input)

//LINT error
// w.Close()
if err := w.Close(); err != nil {
t.Errorf("failed to close pipe: %v", err)
}
os.Stdout = old

out, _ := io.ReadAll(r)
if string(out) != tt.expected {
t.Errorf("got %q, want %q", string(out), tt.expected)
}
})
}
}

func TestSum(t *testing.T) {
tests := []struct {
name string
input []int
expected int
}{
{"positive numbers", []int{1, 2, 3, 4}, 10},
{"with zero", []int{0, 5, 10}, 15},
{"single element", []int{42}, 42},
{"empty slice", []int{}, 0},
{"negative numbers", []int{-1, -2, -3}, -6},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Sum(tt.input)
if result != tt.expected {
t.Errorf("got %d, want %d", result, tt.expected)
}
})
}
}

func TestSort(t *testing.T) {
tests := []struct {
name string
input []int
expected []int
}{
{"already sorted", []int{1, 2, 3}, []int{1, 2, 3}},
{"reversed", []int{3, 2, 1}, []int{1, 2, 3}},
{"unsorted", []int{5, 2, 8, 1}, []int{1, 2, 5, 8}},
{"single element", []int{42}, []int{42}},
{"empty slice", []int{}, []int{}},
{"duplicates", []int{3, 1, 2, 1}, []int{1, 1, 2, 3}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Sort(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("got %v, want %v", result, tt.expected)
}
})
}
}

func ExampleSayHelloTo() {
SayHelloTo("Pavlo")
// Output: Hello, Pavlo!
}

func ExampleSum() {
fmt.Println(Sum([]int{1, 2, 3}))
// Output: 6
}

func ExampleSort() {
fmt.Println(Sort([]int{3, 1, 2}))
// Output: [1 2 3]
}
Binary file added Лабораторна робота №1.pdf
Binary file not shown.