Skip to content

Commit

Permalink
implement 2e2 test (#37)
Browse files Browse the repository at this point in the history
* refacotr unit test structure

* update ci

* init rspec

* implement rspec test

* add Rakefile

* refacotr

* add pain.sh

* update ci

* add e2e test to ci

* add bonus rule to ci

* fix ci name
  • Loading branch information
rask24 committed Feb 14, 2024
1 parent b0a1f8f commit 2e906c4
Show file tree
Hide file tree
Showing 26 changed files with 305 additions and 22 deletions.
35 changes: 29 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip setuptools
Expand All @@ -19,20 +19,43 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
submodules: recursive
- name: Build push_swap
run: make
test:
name: test
- name: Clean push_swap
run: make fclean
- name: Build checker
run: make bonus
unit_test:
name: unit_test
runs-on: macos-12
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
submodules: recursive
- name: Build push_swap
run: make
- name: Test
- name: Unit test
run: make test
e2e_test:
runs-on: macos-12
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: recursive
- name: Build push_swap
run: make
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3.0
- name: Install dependencies
run: |
gem install bundler
bundle install
- name: Run tests
run: rake test
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
inherit_gem:
rubocop-shopify: rubocop.yml
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "rspec"
gem "open3"
gem "rubocop-shopify", require: false
58 changes: 58 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
diff-lcs (1.5.1)
json (2.7.1)
language_server-protocol (3.17.0.3)
open3 (0.2.1)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
racc
racc (1.7.3)
rainbow (3.1.1)
regexp_parser (2.9.0)
rexml (3.2.6)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.0)
rubocop (1.60.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-shopify (2.14.0)
rubocop (~> 1.51)
ruby-progressbar (1.13.0)
unicode-display_width (2.5.0)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
open3
rspec
rubocop-shopify

BUNDLED WITH
2.5.3
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ SRC_DIR = src
BUILD_DIR = build
INC_DIR = include
LIBFT_DIR = libft
TEST_DIR = test
GTEST_DIR = test/gtest
TEST_DIR = test/unit
GTEST_DIR = test/unit/gtest
TEST_BUILD_DIR = test/build

SRC = $(SRC_DIR)/main.c \
Expand Down
16 changes: 16 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:test) do |task|
task.pattern = [
"test/e2e/spec/corner_cases_spec.rb",
"test/e2e/spec/error_cases_spec.rb",
]
end

RSpec::Core::RakeTask.new(:performance) do |task|
task.pattern = [
"test/e2e/spec/performance_spec.rb",
]
end
1 change: 1 addition & 0 deletions test/e2e/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
14 changes: 0 additions & 14 deletions test/e2e/check.rb

This file was deleted.

13 changes: 13 additions & 0 deletions test/e2e/helper/command_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "open3"

def execute_push_swap(*args)
stdout, stderr, status = Open3.capture3("./push_swap #{args.join(" ")}")
[stdout, stderr, status]
end

def execute_checker(*args)
stdout, stderr, status = Open3.capture3("./push_swap #{args.join(" ")} | ./checker #{args.join(" ")}")
[stdout, stderr, status]
end
98 changes: 98 additions & 0 deletions test/e2e/helper/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with(:rspec) do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with(:rspec) do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
# # This allows you to limit a spec run to individual examples or groups
# # you care about by tagging them with `:focus` metadata. When nothing
# # is tagged with `:focus`, all examples get run. RSpec also provides
# # aliases for `it`, `describe`, and `context` that include `:focus`
# # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
# config.filter_run_when_matching :focus
#
# # Allows RSpec to persist some state between runs in order to support
# # the `--only-failures` and `--next-failure` CLI options. We recommend
# # you configure your source control system to ignore this file.
# config.example_status_persistence_file_path = "spec/examples.txt"
#
# # Limits the available syntax to the non-monkey patched syntax that is
# # recommended. For more details, see:
# # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
# config.disable_monkey_patching!
#
# # This setting enables warnings. It's recommended, but in some cases may
# # be too noisy due to issues in dependencies.
# config.warnings = true
#
# # Many RSpec users commonly either run the entire suite or an individual
# # file, and it's useful to allow more verbose output when running an
# # individual spec file.
# if config.files_to_run.one?
# # Use the documentation formatter for detailed output,
# # unless a formatter has already been configured
# # (e.g. via a command-line flag).
# config.default_formatter = "doc"
# end
#
# # Print the 10 slowest examples and example groups at the
# # end of the spec run, to help surface which specs are running
# # particularly slow.
# config.profile_examples = 10
#
# # Run specs in random order to surface order dependencies. If you find an
# # order dependency and want to debug it, you can fix the order by providing
# # the seed, which is printed after each run.
# # --seed 1234
# config.order = :random
#
# # Seed global randomization in this process using the `--seed` CLI option.
# # Setting this allows you to use `--seed` to deterministically reproduce
# # test failures related to randomization by passing the same `--seed` value
# # as the one that triggered the failure.
# Kernel.srand config.seed
end
11 changes: 11 additions & 0 deletions test/e2e/spec/corner_cases_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require_relative "../helper/command_helper"

describe "Corner cases for push_swap" do
it "does not output operations for already sorted input" do
stdout, _stderr, status = execute_push_swap("0", "1", "2", "3")
expect(stdout).to(eq(""))
expect(status.exitstatus).to(eq(0))
end
end
26 changes: 26 additions & 0 deletions test/e2e/spec/error_cases_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require_relative "../helper/command_helper"

describe "Error cases for push_swap" do
it "returns error for non integer inputs" do
_stdout, stderr, status = execute_push_swap("a", "b", "c")

expect(stderr).to(eq("Error\n"))
expect(status.exitstatus).to(eq(1))
end

it "returns error for duplicates" do
_stdout, stderr, status = execute_push_swap("3", "2", "5", "4", "1", "0", "2")

expect(stderr).to(eq("Error\n"))
expect(status.exitstatus).to(eq(1))
end

it "returns error for overflow" do
_stdout, stderr, status = execute_push_swap("0", "3", "2", "34578347984", "1")

expect(stderr).to(eq("Error\n"))
expect(status.exitstatus).to(eq(1))
end
end
26 changes: 26 additions & 0 deletions test/e2e/spec/performance_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require_relative "../helper/command_helper"

describe("Performance test for push_swap") do
size = 500
it "measures operations for random input of size #{size}" do
results = []
20.times do
input = (0...500).to_a.shuffle.map(&:to_s)
stdout, _stderr, status = execute_push_swap(*input)
expect(status.exitstatus).to(eq(0))
results << stdout.split("\n").count

stdout, _stderr, status = execute_checker(*input)
expect(status.exitstatus).to(eq(0))
expect(stdout).to(eq("OK\n"))
print "\e[32m.\e[0m"
end
max_operations = results.max
min_operations = results.min
average_operations = results.sum / results.size.to_f
print "\n"
print "For size #{size}: Max: #{max_operations}, Min: #{min_operations}, Average: #{average_operations}"
end
end
16 changes: 16 additions & 0 deletions test/pain.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
NBR_COUNT=500
##
NBR=$(seq 1 $NBR_COUNT | sort -R | tr '\n' ' ' | rev | cut -c 2- | rev)
#NBR="0 1 2 3 4 5 6 7 8 9"
#NBR=$(seq 1 $NBR_COUNT | tr '\n' ' ' | rev | cut -c 2- | rev)
CMD=$( ./push_swap $NBR)
NBR_LINK=$( echo "$NBR" | tr ' ' ',' )
CMD_LINK=$( echo "$CMD" | sed -e"s/rra/g/g" -e"s/rrb/h/g" \
-e"s/rrr/i/g" -e"s/sa/a/g" -e"s/sb/b/g" -e"s/ss/c/g" \
-e"s/ra/d/g" -e"s/rb/e/g" -e"s/rr/f/g" -e"s/pa/j/g" -e"s/pb/k/g" | tr -d '\n')
# CMD_COUNT=${#CMD_LINK}
RESULT=$( echo $CMD | ./checker $NBR )
echo "Operations: $CMD_COUNT"
echo "Result $RESULT"
open "https://kaaaaakun.github.io/push-swap-pain/?&nbr=$NBR_LINK&cmd=$CMD_LINK"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 2e906c4

Please sign in to comment.