Skip to content

Commit

Permalink
unittest.proto: Add cpp_trigraph field from Google protobuf 2.4.1.
Browse files Browse the repository at this point in the history
wire-format-test.lisp: Rename to wire-format_test.lisp.
protobuf.asd: Reflow comments.
  • Loading branch information
brown committed Jan 27, 2012
1 parent d6d4c9a commit 5b32488
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 23 deletions.
7 changes: 7 additions & 0 deletions google/protobuf/unittest.proto
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -495,6 +495,13 @@ message TestExtremeDefaultValues {
// optional float inf_float = 17 [default = inf]; // optional float inf_float = 17 [default = inf];
// optional float neg_inf_float = 18 [default = -inf]; // optional float neg_inf_float = 18 [default = -inf];
// optional float nan_float = 19 [default = nan]; // optional float nan_float = 19 [default = nan];

// Tests for C++ trigraphs.
// Trigraphs should be escaped in C++ generated files, but they should not be
// escaped for other languages.
// Note that in .proto file, "\?" is a valid way to escape ? in string
// literals.
optional string cpp_trigraph = 20 [default = "? \? ?? \?? \??? ??/ ?\?-"];
} }


message SparseEnumMessage { message SparseEnumMessage {
Expand Down
2 changes: 1 addition & 1 deletion protobuf-test.asd
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
:components :components
((:static-file "golden") ((:static-file "golden")
(:file "message-test" :depends-on ("unittest")) (:file "message-test" :depends-on ("unittest"))
(:file "wire-format-test") (:file "wire-format_test")
;; Old protocol buffer tests. ;; Old protocol buffer tests.
;; TODO(brown): Delete when the new proto2 tests cover all the functionality. ;; TODO(brown): Delete when the new proto2 tests cover all the functionality.
(:file "proto-lisp-test" :depends-on ("testproto1" "testproto2")) (:file "proto-lisp-test" :depends-on ("testproto1" "testproto2"))
Expand Down
43 changes: 21 additions & 22 deletions protobuf.asd
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@
:initform () :initform ()
:initarg :proto-search-path :initarg :proto-search-path
:reader search-path :reader search-path
:documentation "List containing directories where the protocol buffer compiler should search :documentation
for imported protobuf files. Non-absolute pathnames are treated as relative to the directory "List containing directories where the protocol buffer compiler should search
containing the DEFSYSTEM form in which they appear.")) for imported protobuf files. Non-absolute pathnames are treated as relative to
the directory containing the DEFSYSTEM form in which they appear."))
(:documentation "A protocol buffer definition file.")) (:documentation "A protocol buffer definition file."))


(export '(protobuf-source-file proto-pathname search-path)) (export '(protobuf-source-file proto-pathname search-path))
Expand All @@ -91,25 +92,26 @@ containing the DEFSYSTEM form in which they appear."))


(defclass proto-to-lisp (operation) (defclass proto-to-lisp (operation)
() ()
(:documentation "An ASDF operation that compiles a .proto file containing protocol buffer (:documentation
"An ASDF operation that compiles a .proto file containing protocol buffer
definitions into a Lisp source file.")) definitions into a Lisp source file."))


(defmethod component-depends-on ((operation compile-op) (component protobuf-source-file)) (defmethod component-depends-on ((operation compile-op) (component protobuf-source-file))
"Compiling a protocol buffer file depends on generating Lisp source code for the protobuf, but "Compiling a protocol buffer file depends on generating Lisp source code for
also on loading package definitions and in-line function definitions that the machine-generated the protobuf, but also on loading package definitions and in-line function
protobuf Lisp code uses." definitions that the machine-generated protobuf Lisp code uses."
`((proto-to-lisp ,(component-name component)) `((proto-to-lisp ,(component-name component))
,@(call-next-method))) ,@(call-next-method)))


(defmethod component-depends-on ((operation load-op) (component protobuf-source-file)) (defmethod component-depends-on ((operation load-op) (component protobuf-source-file))
"Loading a protocol buffer file depends on generating Lisp source code for the protobuf, but also "Loading a protocol buffer file depends on generating Lisp source code for the
on loading package definitions and in-line function definitions that the machine-generated protobuf protobuf, but also on loading package definitions and in-line function
Lisp code uses." definitions that the machine-generated protobuf Lisp code uses."
`((proto-to-lisp ,(component-name component)) `((proto-to-lisp ,(component-name component))
,@(call-next-method))) ,@(call-next-method)))


(defun proto-input (protobuf-source-file) (defun proto-input (protobuf-source-file)
"Return the pathname of the protocol buffer definition file that must be "Returns the pathname of the protocol buffer definition file that must be
translated into Lisp source code for this PROTO-FILE component." translated into Lisp source code for this PROTO-FILE component."
(if (proto-pathname protobuf-source-file) (if (proto-pathname protobuf-source-file)
;; Path of the protobuf file was specified with :PROTO-PATHNAME. ;; Path of the protobuf file was specified with :PROTO-PATHNAME.
Expand All @@ -126,13 +128,14 @@ translated into Lisp source code for this PROTO-FILE component."
(list *protoc* *protoc-gen-lisp* (proto-input component))) (list *protoc* *protoc-gen-lisp* (proto-input component)))


(defmethod output-files ((operation proto-to-lisp) (component protobuf-source-file)) (defmethod output-files ((operation proto-to-lisp) (component protobuf-source-file))
"Arrange for the Lisp output file of PROTO-TO-LISP operations to be stored where fasl files are "Arranges for the Lisp output file of PROTO-TO-LISP operations to be stored
located." where fasl files are located."
(values (list (component-pathname component)) (values (list (component-pathname component))
nil)) ; allow around methods to translate nil)) ; allow around methods to translate


(defun resolve-relative-pathname (path parent-path) (defun resolve-relative-pathname (path parent-path)
"When PATH doesn't have an absolute directory component, treat it as relative to PARENT-PATH." "When PATH doesn't have an absolute directory component, treat it as relative
to PARENT-PATH."
(let* ((pathname (pathname path)) (let* ((pathname (pathname path))
(directory (pathname-directory pathname))) (directory (pathname-directory pathname)))
(if (and (list directory) (eq (car directory) :absolute)) (if (and (list directory) (eq (car directory) :absolute))
Expand Down Expand Up @@ -175,9 +178,9 @@ located."
(error 'compile-failed :component component :operation operation)))) (error 'compile-failed :component component :operation operation))))


(defmethod asdf::component-self-dependencies :around ((op load-op) (c protobuf-source-file)) (defmethod asdf::component-self-dependencies :around ((op load-op) (c protobuf-source-file))
"Remove PROTO-TO-LISP operations from self dependencies. Otherwise, the Lisp output files of "Removes PROTO-TO-LISP operations from self dependencies. Otherwise, the Lisp
PROTO-TO-LISP are considered to be input files for LOAD-OP, which means ASDF loads both the .lisp output files of PROTO-TO-LISP are considered to be input files for LOAD-OP,
file and the .fasl file." which means ASDF loads both the .lisp file and the .fasl file."
(remove-if (lambda (x) (remove-if (lambda (x)
(eq (car x) 'proto-to-lisp)) (eq (car x) 'proto-to-lisp))
(call-next-method))) (call-next-method)))
Expand Down Expand Up @@ -214,13 +217,9 @@ file and the .fasl file."
:name "Protocol Buffer" :name "Protocol Buffer"
:description "Protocol buffer code" :description "Protocol buffer code"
:long-description "A Common Lisp implementation of Google's protocol buffer support libraries." :long-description "A Common Lisp implementation of Google's protocol buffer support libraries."
:version "0.7.3" :version "0.7.4"
:author "Robert Brown" :author "Robert Brown"
:license "See file COPYING and the copyright messages in individual files." :license "See file COPYING and the copyright messages in individual files."
;; After loading the system, announce its availability.
:perform (load-op :after (operation component)
(pushnew :protobuf cl:*features*)
(provide 'protobuf))
:depends-on (com.google.base :depends-on (com.google.base
varint varint
#-(or allegro clisp sbcl) trivial-utf-8) #-(or allegro clisp sbcl) trivial-utf-8)
Expand Down
94 changes: 94 additions & 0 deletions wire-format_test.lisp
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,94 @@
;;;; Copyright 2010 Google Inc. All Rights Reserved

;;;; Redistribution and use in source and binary forms, with or without
;;;; modification, are permitted provided that the following conditions are
;;;; met:

;;;; * Redistributions of source code must retain the above copyright
;;;; notice, this list of conditions and the following disclaimer.
;;;; * Redistributions in binary form must reproduce the above
;;;; copyright notice, this list of conditions and the following disclaimer
;;;; in the documentation and/or other materials provided with the
;;;; distribution.
;;;; * Neither the name of Google Inc. nor the names of its
;;;; contributors may be used to endorse or promote products derived from
;;;; this software without specific prior written permission.

;;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;;;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;;;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;;;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;;; Author: brown@google.com (Robert Brown)

;;;; Test the protocol buffer wire format.

(in-package #:common-lisp-user)

(defpackage #:wire-format-test
(:documentation "Tests for protocol buffer wire format functions.")
(:use #:common-lisp
#:com.google.base
#:hu.dwim.stefil
#:wire-format)
(:export #:test-wire-format))

(in-package #:wire-format-test)
(declaim #.*optimize-default*)

(defsuite (test-wire-format :in root-suite) ()
(run-child-tests))

(in-suite test-wire-format)

(deftest zig-zag-encode-decode ()
(flet ((verify (fun arg-results)
(loop for (arg result) in arg-results do
(is (= (funcall fun arg) result)))))
(verify #'zig-zag-encode32
`((0 0) (-1 1) (1 2) (-2 3)
(#x3fffffff #x7ffffffe)
(,(- #xc0000000 (ash 1 32)) #x7fffffff)
(#x7fffffff #xfffffffe)
(,(- #x80000000 (ash 1 32)) #xffffffff)))
(verify #'zig-zag-decode32
`((0 0) (1 -1) (2 1) (3 -2)
(#x7ffffffe #x3fffffff)
(#x7fffffff ,(- #xc0000000 (ash 1 32)))
(#xfffffffe #x7fffffff)
(#xffffffff ,(- #x80000000 (ash 1 32)))))
(verify #'zig-zag-encode64
`((0 0) (-1 1) (1 2) (-2 3)
(#x000000003fffffff #x000000007ffffffe)
(,(- #xffffffffc0000000 (ash 1 64)) #x000000007fffffff)
(#x000000007fffffff #x00000000fffffffe)
(,(- #xffffffff80000000 (ash 1 64)) #x00000000ffffffff)
(#x7fffffffffffffff #xfffffffffffffffe)
(,(- #x8000000000000000 (ash 1 64)) #xffffffffffffffff)))
(verify #'zig-zag-decode64
`((0 0) (1 -1) (2 1) (3 -2)
(#x000000007ffffffe #x000000003fffffff)
(#x000000007fffffff ,(- #xffffffffc0000000 (ash 1 64)))
(#x00000000fffffffe #x000000007fffffff)
(#x00000000ffffffff ,(- #xffffffff80000000 (ash 1 64)))
(#xfffffffffffffffe #x7fffffffffffffff)
(#xffffffffffffffff ,(- #x8000000000000000 (ash 1 64))))))

;; Some easier-to-verify round-trip tests. The inputs (other than 0, 1, -1) were chosen
;; semi-randomly via keyboard bashing.
(flet ((round-trip32 (n)
(is (= n (zig-zag-decode32 (zig-zag-encode32 n)))))
(round-trip64 (n)
(is (= n (zig-zag-decode64 (zig-zag-encode64 n))))))
(dolist (n '(0 1 -1 14927 -3612))
(round-trip32 n))
(dolist (n '(0 1 -1 14927 -3612 856912304801416 -75123905439571256))
(round-trip64 n)))
(values))

0 comments on commit 5b32488

Please sign in to comment.