diff --git a/vimscript/README.md b/vimscript/README.md index 2b451346..5f15d986 100644 --- a/vimscript/README.md +++ b/vimscript/README.md @@ -20,3 +20,4 @@ - [reverse-string](./reverse-string/README.md) - [roman-numerals](./roman-numerals/README.md) - [scrabble-score](./scrabble-score/README.md) +- [difference-of-squares](./difference-of-squares/README.md) diff --git a/vimscript/difference-of-squares/.coverage_covimerage b/vimscript/difference-of-squares/.coverage_covimerage new file mode 100644 index 00000000..747ac731 --- /dev/null +++ b/vimscript/difference-of-squares/.coverage_covimerage @@ -0,0 +1 @@ +!coverage.py: This is a private format, don't read it directly!{"lines":{"/home/vpayno/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim":[14,18,24]},"file_tracers":{"/home/vpayno/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim":"covimerage.CoveragePlugin"}} \ No newline at end of file diff --git a/vimscript/difference-of-squares/.coveragerc b/vimscript/difference-of-squares/.coveragerc new file mode 100644 index 00000000..8cddf91c --- /dev/null +++ b/vimscript/difference-of-squares/.coveragerc @@ -0,0 +1,3 @@ +[run] +plugins = covimerage +data_file = .coverage_covimerage diff --git a/vimscript/difference-of-squares/.themisrc b/vimscript/difference-of-squares/.themisrc new file mode 100644 index 00000000..5c43c80e --- /dev/null +++ b/vimscript/difference-of-squares/.themisrc @@ -0,0 +1,17 @@ +" .themisrc + +let g:repo_root = fnamemodify(expand(''), ':h:h') + +call themis#option('exclude', g:repo_root . '/*.md') +call themis#option('exclude', g:repo_root . '/*.vader') +call themis#option('exclude', g:repo_root . '/*.txt') +call themis#helper('command').with(themis#helper('assert')) + +if $PROFILE_LOG !=# '' + execute 'profile' 'start' $PROFILE_LOG + execute 'profile!' 'file' g:repo_root . '/*.vim' +endif + +call themis#option('runtimepath', expand(g:repo_root)) + +" vim:ft=vim diff --git a/vimscript/difference-of-squares/README.md b/vimscript/difference-of-squares/README.md index b4af0f0c..7d5d44cc 100644 --- a/vimscript/difference-of-squares/README.md +++ b/vimscript/difference-of-squares/README.md @@ -31,4 +31,12 @@ Finding the best algorithm for the problem is a key skill in software engineerin ### Based on -Problem 6 at Project Euler - https://projecteuler.net/problem=6 \ No newline at end of file +Problem 6 at Project Euler - https://projecteuler.net/problem=6 + +### My Solution + +- [my solution](./difference_of_squares.vim) +- [vader tests](./difference_of_squares.vader) +- [themis tests](./themis.vimspec) +- [themis profile](./profile.txt) +- [run-tests output](./run-tests-vimscript.txt) diff --git a/vimscript/difference-of-squares/coverage.xml b/vimscript/difference-of-squares/coverage.xml new file mode 100644 index 00000000..c5ebe604 --- /dev/null +++ b/vimscript/difference-of-squares/coverage.xml @@ -0,0 +1,26 @@ + + + + + + /home/vpayno/git_vpayno/exercism-workspace/vimscript/difference-of-squares + + + + + + + + + + + + + + + + + + + + diff --git a/vimscript/difference-of-squares/difference_of_squares.vim b/vimscript/difference-of-squares/difference_of_squares.vim index 84eca3c4..a04cf0a4 100644 --- a/vimscript/difference-of-squares/difference_of_squares.vim +++ b/vimscript/difference-of-squares/difference_of_squares.vim @@ -12,13 +12,15 @@ " 22 " function! DifferenceOfSquares(number) abort - " your implementation goes here + return SquareOfSum(a:number) - SumOfSquares(a:number) endfunction function! SquareOfSum(number) abort - " your implementation goes here + let l:result = a:number * (a:number + 1) / 2 + + return l:result * l:result endfunction function! SumOfSquares(number) abort - " your implementation goes here + return a:number * (a:number + 1) * (2 * a:number + 1) / 6 endfunction diff --git a/vimscript/difference-of-squares/profile.txt b/vimscript/difference-of-squares/profile.txt new file mode 100644 index 00000000..f4b74bb5 --- /dev/null +++ b/vimscript/difference-of-squares/profile.txt @@ -0,0 +1,74 @@ +SCRIPT /home/vpayno/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim +Sourced 1 time +Total time: 0.000017730 + Self time: 0.000017730 + +count total (s) self (s) + " + " Find the difference between the square of the sum and the sum of the squares + " of the first N natural numbers. + " + " Examples: + " + " :echo SquareOfSum(3) + " 36 + " :echo SumOfSquares(3) + " 14 + " :echo DifferenceOfSquares(3) + " 22 + " + 1 0.000003921 function! DifferenceOfSquares(number) abort + return SquareOfSum(a:number) - SumOfSquares(a:number) + endfunction + + 1 0.000000749 function! SquareOfSum(number) abort + let l:result = a:number * (a:number + 1) / 2 + + return l:result * l:result + endfunction + + 1 0.000000887 function! SumOfSquares(number) abort + return a:number * (a:number + 1) * (2 * a:number + 1) / 6 + endfunction + +FUNCTION SumOfSquares() + Defined: ~/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim:24 +Called 6 times +Total time: 0.000010721 + Self time: 0.000010721 + +count total (s) self (s) + 6 0.000010011 return a:number * (a:number + 1) * (2 * a:number + 1) / 6 + +FUNCTION DifferenceOfSquares() + Defined: ~/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim:14 +Called 3 times +Total time: 0.000024316 + Self time: 0.000009151 + +count total (s) self (s) + 3 0.000023471 0.000008306 return SquareOfSum(a:number) - SumOfSquares(a:number) + +FUNCTION SquareOfSum() + Defined: ~/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim:18 +Called 6 times +Total time: 0.000018945 + Self time: 0.000018945 + +count total (s) self (s) + 6 0.000010624 let l:result = a:number * (a:number + 1) / 2 + + 6 0.000005401 return l:result * l:result + +FUNCTIONS SORTED ON TOTAL TIME +count total (s) self (s) function + 3 0.000024316 0.000009151 DifferenceOfSquares() + 6 0.000018945 SquareOfSum() + 6 0.000010721 SumOfSquares() + +FUNCTIONS SORTED ON SELF TIME +count total (s) self (s) function + 6 0.000018945 SquareOfSum() + 6 0.000010721 SumOfSquares() + 3 0.000024316 0.000009151 DifferenceOfSquares() + diff --git a/vimscript/difference-of-squares/run-tests-vimscript.txt b/vimscript/difference-of-squares/run-tests-vimscript.txt new file mode 100644 index 00000000..40f9e80b --- /dev/null +++ b/vimscript/difference-of-squares/run-tests-vimscript.txt @@ -0,0 +1,126 @@ +Running automated test file(s): + + +=============================================================================== + +Running: vint --warning --verbose --enable-neovim . +vint DEBUG: checking: `difference_of_squares.vim` +vint DEBUG: severity: WARNING +vint DEBUG: disabled: ProhibitAbbreviationOption +vint DEBUG: enabled: ProhibitAutocmdWithNoGroup +vint DEBUG: enabled: ProhibitCommandRelyOnUser +vint DEBUG: enabled: ProhibitCommandWithUnintendedSideEffect +vint DEBUG: enabled: ProhibitEncodingOptionAfterScriptEncoding +vint DEBUG: enabled: ProhibitEqualTildeOperator +vint DEBUG: enabled: ProhibitImplicitScopeBuiltinVariable +vint DEBUG: disabled: ProhibitImplicitScopeVariable +vint DEBUG: enabled: ProhibitInvalidMapCall +vint DEBUG: enabled: ProhibitMissingScriptEncoding +vint DEBUG: enabled: ProhibitNoAbortFunction +vint DEBUG: enabled: ProhibitSetNoCompatible +vint DEBUG: enabled: ProhibitUnnecessaryDoubleQuote +vint DEBUG: disabled: ProhibitUnusedVariable +vint DEBUG: enabled: ProhibitUsingUndeclaredVariable + +real 0m0.314s +user 0m0.201s +sys 0m0.115s + +=============================================================================== + +Running: vim '+source *vim | Vader!*' ./*.vader && echo Success || echo Failure +Vim: Warning: Output is not to a terminal +[?1049h[?1h=[?2004h[?25l"./difference_of_squares.vader" 46L, 1081B [?2004l[?1l>[?1049l[?25hVader note: cannot print to stderr reliably/directly. Please consider using Vim's -es/-Es option (mode=n). +VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Jan 24 2024 13:08:58) +Included patches: 1-50 +Compiled by vpayno@penguin +Huge version with GTK2 GUI. Features included (+) or not (-): ++acl +cmdline_hist +ex_extra +jumplist +mouse_dec +perl/dyn -sodium +textobjects +wildmenu ++arabic +cmdline_info +extra_search +keymap -mouse_gpm +persistent_undo +sound +textprop +windows ++autocmd +comments -farsi +lambda -mouse_jsbterm +popupwin +spell +timers +writebackup ++autochdir +conceal +file_in_path +langmap +mouse_netterm +postscript +startuptime +title +X11 +-autoservername +cryptv +find_in_path +libcall +mouse_sgr +printer +statusline +toolbar +xattr ++balloon_eval +cscope +float +linebreak -mouse_sysmouse +profile -sun_workshop +user_commands -xfontset ++balloon_eval_term +cursorbind +folding +lispindent +mouse_urxvt -python +syntax +vartabs +xim ++browse +cursorshape -footer +listcmds +mouse_xterm +python3/dyn +tag_binary +vertsplit -xpm +++builtin_terms +dialog_con_gui +fork() +localmap +multi_byte +quickfix -tag_old_static +vim9script +xsmp_interact ++byte_offset +diff +gettext +lua/dyn +multi_lang +reltime -tag_any_white +viminfo +xterm_clipboard ++channel +digraphs -hangul_input +menu -mzscheme +rightleft +tcl/dyn +virtualedit -xterm_save ++cindent +dnd +iconv +mksession +netbeans_intg +ruby +termguicolors +visual ++clientserver -ebcdic +insert_expand +modify_fname +num64 +scrollbind +terminal +visualextra ++clipboard +emacs_tags +ipv6 +mouse +packages +signs +terminfo +vreplace ++cmdline_compl +eval +job +mouseshape +path_extra +smartindent +termresponse +wildignore + system vimrc file: "$VIM/vimrc" + user vimrc file: "$HOME/.vimrc" + 2nd user vimrc file: "~/.vim/vimrc" + user exrc file: "$HOME/.exrc" + system gvimrc file: "$VIM/gvimrc" + user gvimrc file: "$HOME/.gvimrc" +2nd user gvimrc file: "~/.vim/gvimrc" + defaults file: "$VIMRUNTIME/defaults.vim" + system menu file: "$VIMRUNTIME/menu.vim" + fall-back for $VIM: "/home/vpayno/.local/vim/usr/share/vim" + f-b for $VIMRUNTIME: "/home/vpayno/.local/vim/usr/share/vim/vim82" +Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK -pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16 -g -O2 -D_REENTRANT -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 +Linking: gcc -Wl,-E -rdynamic -Wl,-export-dynamic -L/usr/local/lib -Wl,--as-needed -o vim -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lharfbuzz -lfontconfig -lfreetype -lSM -lICE -lXt -lX11 -lXdmcp -lSM -lICE -lm -ltinfo -lselinux -lcanberra -lrt -ldl -L/home/vpayno/.local/activetcl-8.6/lib -ltclstub8.6 -ldl -lz -lpthread -lm -Wl,-rpath,/home/vpayno/.rbenv/versions/3.1.1/lib -L/home/vpayno/.rbenv/versions/3.1.1/lib -lruby -lm + +Starting Vader: 1 suite(s), 9 case(s) + Starting Vader: /home/vpayno/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vader + (1/9) [EXECUTE] square of sum 1 + (2/9) [EXECUTE] square of sum 5 + (3/9) [EXECUTE] square of sum 100 + (4/9) [EXECUTE] sum of squares 1 + (5/9) [EXECUTE] sum of squares 5 + (6/9) [EXECUTE] sum of squares 100 + (7/9) [EXECUTE] difference of squares 1 + (8/9) [EXECUTE] difference of squares 5 + (9/9) [EXECUTE] difference of squares 100 + Success/Total: 9/9 +Success/Total: 9/9 (assertions: 9/9) +Elapsed time: 0.12 sec. +[?1049h[?1h=[?2004h[?2004l[?2004h[?2004l[?2004l[?1l>[?1049l +real 0m2.545s +user 0m0.335s +sys 0m0.147s +Success + +=============================================================================== + +Running: themis ./themis.vimspec +1..3 +ok 1 - thesis tests test_DifferenceOfSquares +ok 2 - thesis tests test_SquareOfSum +ok 3 - thesis tests test_SumOfSquares + +# tests 3 +# passes 3 + +real 0m0.032s +user 0m0.020s +sys 0m0.008s + +=============================================================================== + +Script line does not match function line, ignoring: ' return a:number * (a:number + 1) * (2 * a:number + 1) / 6' != '10011 return a:number * (a:number + 1) * (2 * a:number + 1) / 6'. +Script line does not match function line, ignoring: ' return SquareOfSum(a:number) - SumOfSquares(a:number)' != '08306 return SquareOfSum(a:number) - SumOfSquares(a:number)'. +Script line does not match function line, ignoring: ' let l:result = a:number * (a:number + 1) / 2' != '10624 let l:result = a:number * (a:number + 1) / 2'. +Could not find source for function: SumOfSquares +Could not find source for function: DifferenceOfSquares +Could not find source for function: SquareOfSum +Writing coverage file .coverage_covimerage. +Coverage.py warning: Plugin file tracers (covimerage.CoveragePlugin) aren't supported with PyTracer +Name Stmts Miss Cover +----------------------------------------------- +difference_of_squares.vim 7 4 43% +Coverage.py warning: Plugin file tracers (covimerage.CoveragePlugin) aren't supported with PyTracer + +=============================================================================== + +Running: misspell . + +real 0m0.019s +user 0m0.012s +sys 0m0.019s + +=============================================================================== + diff --git a/vimscript/difference-of-squares/themis.vimspec b/vimscript/difference-of-squares/themis.vimspec new file mode 100644 index 00000000..ae9e087d --- /dev/null +++ b/vimscript/difference-of-squares/themis.vimspec @@ -0,0 +1,80 @@ +" themis.vimspec + +let s:suite = themis#suite('thesis tests') +let s:assert = themis#helper('assert') + +source *.vim + +" SquareOfSum +let g:test_cases1 = { + \ 1: 1, + \ 5: 225, + \ 100: 25502500 + \ } + +" SumOfSquares +let g:test_cases2 = { + \ 1: 1, + \ 5: 55, + \ 100: 338350 + \ } + +" DifferenceOfSquares +let g:test_cases3 = { + \ 1: 0, + \ 5: 170, + \ 100: 25164150 + \ } + +" The function name(my_test_1) will be a test name. +function s:suite.test_SquareOfSum() + let l:number = 0 + let l:result = 0 + + for l:key in keys(g:test_cases1) + let l:number = l:key + let l:result = g:test_cases1[l:key] + + let l:got = SquareOfSum(l:number) + let l:want = l:result + + " appending the number to the want/got values to make it easier to debug failures + call s:assert.equals(l:number . ':' . l:want, l:number . ':' . l:got) + endfor +endfunction + +" The function name(my_test_1) will be a test name. +function s:suite.test_SumOfSquares() + let l:number = 0 + let l:result = 0 + + for l:key in keys(g:test_cases2) + let l:number = l:key + let l:result = g:test_cases2[l:key] + + let l:got = SumOfSquares(l:number) + let l:want = l:result + + " appending the number to the want/got values to make it easier to debug failures + call s:assert.equals(l:number . ':' . l:want, l:number . ':' . l:got) + endfor +endfunction + +" The function name(my_test_1) will be a test name. +function s:suite.test_DifferenceOfSquares() + let l:number = 0 + let l:result = 0 + + for l:key in keys(g:test_cases3) + let l:number = l:key + let l:result = g:test_cases3[l:key] + + let l:got = DifferenceOfSquares(l:number) + let l:want = l:result + + " appending the number to the want/got values to make it easier to debug failures + call s:assert.equals(l:number . ':' . l:want, l:number . ':' . l:got) + endfor +endfunction + +" vim: ft=vim